******This file generates a consistent set of category expenditures and individual products (quantities and expenditures) across all of the NSS rounds for use in the Variety paper


clear
set mem 5g


*Set root data directory
local rootdir 
cd "`rootdir'"




******Step 0: multipliers, common regions and labels*******
cd "`rootdir'round38\"
***Add multiplier and other common linking variables***
use round38_chh, clear
*143 duplicates;  117,604 households
duplicates drop sector subround region subsample fsu hhno, force
*egen totpop=total(mult*hhsize)
*gives total population of about 667 million
gen mult=multcomb
keep sector region stratum subround subsample fsu hhno mult
gen round=38
gen segment=1
gen substratum=1
save "`rootdir'mult38", replace

cd "`rootdir'\Round43"
***Add multiplier and other common linking variables***
use round43_chh, clear
*zero duplicates;   128,019 households
duplicates drop sector fsu region subsample subround substratum hhno, force
*egen totpop=total(hhsize*mult)
*gives total population of about 719 million
keep sector fsu region subsample subround substratum hhno district mult
gen round=43
gen segment=1
save "`rootdir'mult43", replace



cd "`rootdir'Round55\"
use round55_chh, clear
*zero duplicates;   120,314 households
duplicates drop sector region subround subsample fsu segment substratum hhno, force
egen totpop=total(hhsize*mult_ss/800)
*gives total population of about 919 million
gen mult=mult_ss/800
keep sector region subround subsample fsu segment substratum hhno mult district
gen round=55
save "`rootdir'mult55", replace

cd "`rootdir'\round61\"

use round61_chh, clear
*zero duplicates;   124,644 households
duplicates drop sector region subround subsample segment substratum fsu hhno, force
replace mult=mult/200
*egen totpop=total(hhsize*mult)
*gives total population of about 980 million
keep sector region subround subsample segment substratum fsu hhno mult district
gen round=61
save "`rootdir'mult61", replace

cd "`rootdir'round66"
use round661_chh, clear
*zero duplicates;   100,855 households
duplicates drop sector region subround subsample segment substratum fsu hhno, force
gen match=1
replace match=2 if nss~=nsc
replace mult=mult/(match*100)
*egen totpop=total(hhsize*(mult))
*gives total population of about 1.04 billion
keep sector region subround subsample segment substratum fsu hhno mult district
gen round=66
save "`rootdir'mult661", replace



use mult38, clear
gen statereg38=region

# delimit ;
	gen regioncode=.;
	*converting to regions used in the 55th round;
	replace regioncode=21	if statereg38==21;
	replace regioncode=22	if statereg38==22;
	replace regioncode=23	if statereg38==23;
	replace regioncode=24	if statereg38==24;
	replace regioncode=31	if statereg38==251;
	replace regioncode=41	if statereg38==31;
	replace regioncode=42	if statereg38==32;
	replace regioncode=43	if statereg38==33;
	replace regioncode=51	if statereg38==41;
	replace regioncode=52	if statereg38==42;
	replace regioncode=53	if statereg38==43;
	replace regioncode=71	if statereg38==51;
	replace regioncode=72	if statereg38==52;
	replace regioncode=73	if statereg38==53;
	replace regioncode=74	if statereg38==54;
	replace regioncode=75	if statereg38==55;
	replace regioncode=81	if statereg38==61;
	replace regioncode=82	if statereg38==62;
	replace regioncode=91	if statereg38==71;
	replace regioncode=101	if statereg38==81;
	replace regioncode=102	if statereg38==82;
	replace regioncode=103	if statereg38==83;
	replace regioncode=111	if statereg38==91;
	replace regioncode=112	if statereg38==92;
	replace regioncode=113	if statereg38==93;
	replace regioncode=114	if statereg38==94;
	replace regioncode=121	if statereg38==101;
	replace regioncode=122	if statereg38==102;
	replace regioncode=131	if statereg38==111;
	replace regioncode=132	if statereg38==112;
	replace regioncode=133	if statereg38==113;
	replace regioncode=134	if statereg38==114;
	replace regioncode=135	if statereg38==115;
	replace regioncode=136	if statereg38==116;
	replace regioncode=137	if statereg38==117;
	replace regioncode=141	if statereg38==121;
	replace regioncode=142	if statereg38==122;
	replace regioncode=143	if statereg38==123;
	replace regioncode=144	if statereg38==124;
	replace regioncode=145	if statereg38==125;
	replace regioncode=146	if statereg38==126;
	replace regioncode=151	if statereg38==131;
	replace regioncode=152	if statereg38==132;
	replace regioncode=161	if statereg38==141;
	replace regioncode=171	if statereg38==311;
	replace regioncode=181	if statereg38==151;
	replace regioncode=191	if statereg38==161;
	replace regioncode=192	if statereg38==162;
	replace regioncode=193	if statereg38==163;
	replace regioncode=201	if statereg38==171;
	replace regioncode=202	if statereg38==172;
	replace regioncode=211	if statereg38==181;
	replace regioncode=212	if statereg38==182;
	replace regioncode=213	if statereg38==183;
	replace regioncode=214	if statereg38==184;
	replace regioncode=221	if statereg38==191;
	replace regioncode=231	if statereg38==201;
	replace regioncode=232	if statereg38==202;
	replace regioncode=233	if statereg38==203;
	replace regioncode=234	if statereg38==204;
	replace regioncode=241	if statereg38==211;
	replace regioncode=251	if statereg38==221;
	replace regioncode=252	if statereg38==222;
	replace regioncode=253	if statereg38==223;
	replace regioncode=254	if statereg38==224;
	replace regioncode=255	if statereg38==225;
	replace regioncode=261	if statereg38==231;
	replace regioncode=262	if statereg38==232;
	replace regioncode=263	if statereg38==233;
	replace regioncode=264	if statereg38==234;
	replace regioncode=271	if statereg38==241;
	replace regioncode=281	if statereg38==261;
	replace regioncode=291	if statereg38==271;
	replace regioncode=301	if statereg38==291;
	replace regioncode=311	if statereg38==281;
	replace regioncode=321	if statereg38==301;
	replace regioncode=331	if statereg38==321;

	
	*combining first three Gujarat regions into one for consistency;
	replace regioncode=71 if regioncode==72;
	replace regioncode=71 if regioncode==73;

	cap label drop states;
label define states

2	"Andhra Pradesh   "
3	"Assam            "
4	"Bihar            "
5	"Gujarat          "
6	"Haryana          "
7	"Himachal Pradesh "
8	"Jammu &Kashmir   "
9	"Karnataka        "
10	"Kerala           "
11	"Madhya Pradesh   "
12	"Maharashtra      "
13	"Manipur          "
14	"Meghalaya        "
15	"Nagaland         "
16	"Orissa           "
17	"Punjab		  "
18	"Rajasthan	  "	
19	"Sikkim           "
20	"Tamil Nadu	  "
21	"Tripura	  "
22	"Uttar Pradesh	  "
23	"West Bengal	  "
24	"Andaman & Nicobar Islands "
25	"Arunachal Pradesh"
26	"Chandigarh	  "
27	"Dadra &Nagar Haveli"
28	"Delhi            "
29	"Goa, Daman & Diu "
30	"Lakshadweep      "
31	"Mizoram          "
32	"Pondicherry      ";

cap label drop regions;
label define regions 

21	"Andhra Pradesh Costal"
22	"Andhra Pradesh Inland Northern"
23	"Andhra Pradesh South -Western"
24	"Andhra Pradesh Inland Southern"
31	"Arunachal Pradesh"
41	"Assam Plains Western"
42	"Assam Plains Western"
43	"Assam Hills"
51	"Bihar Southern"
52	"Bihar Northern"
53	"Bihar Central"
71	"Gujarat Plains and South-Eastern"
74	"Gujarat Dry areas"
75	"Gujarat Saurashtra"
81	"Haryana Eastern"
82	"Haryana Western"
91	"Himachal Pradesh"
101	"Jammu &Kashmir Mountainous"
102	"Jammu &Kashmir Outer Hills"
103	"Jammu &Kashmir Jhelam Valley"
111	"Karnataka Coastal & Ghats"
112	"Karnataka Inland Eastern"
113	"Karnataka Inland Southern"
114	"Karnataka Inland Nothern"
121	"Kerala Northern"
122	"Kerala Southern"
131	"Madhya Pradesh Chattisgarh"
132	"Madhya Pradesh Vindhya"
133	"Madhya Pradesh Central"
134	"Madhya Pradesh Malwa"
135	"Madhya Pradesh South"
136	"Madhya Pradesh South Western"
137	"Madhya Pradesh Northern"
141	"Maharashtra Coastal"
142	"Maharashtra Inland Western"
143	"Maharashtra Inland Northern"
144	"Maharashtra Inland Central"
145	"Maharashtra Inland Eastern"
146	"Maharashtra Eastern"
151	"Manipur Plains"
152	"Manipur Hills"
161	"Meghalaya"
171	"Mizoram"
181	"Nagaland"
191	"Orissa Coastal"
192	"Orissa Southern"
193	"Orissa Northern"
201	"Punjab Northern"
202	"Punjab Southern"
211	"Rajasthan Western"
212	"Rajasthan North-Eastern"
213	"Rajasthan Southern"
214	"Rajasthan South-Eastern"
221	"Sikkim"
231	"Tamil Nadu Coastal Northern"
232	"Tamil Nadu Coastal"
233	"Tamil Nadu Southern"
234	"Tamil Nadu Inland"
241	"Tripura"
251	"Uttar Pradesh Himalayan"
252	"Uttar Pradesh Western"
253	"Uttar Pradesh Central"
254	"Uttar Pradesh Eastern"
255	"Uttar Pradesh Southern"
261	"West Bengal Himalayan"
262	"West Bengal Eastern Plains"
263	"West Bengal Central Plains"
264	"West Bengal Western Plains"
271	"Andaman & Nicobar Islands"
281	"Chandigarh"
291	"Dadra &Nagar Haveli"
301	"Goa & Daman & Diu"
311	"Delhi"
321	"Lakshadweep"
331	"Pondicherry";

	# delimit cr
	
	gen statecode=floor(regioncode/10)
	drop statereg38

save mult38, replace



use mult43, clear

gen statereg43=region
gen dist_code43=district
gen districtcode=district

# delimit ;
*converting to regions used in the 55th round;
	gen regioncode=.;
	replace regioncode=21 if statereg43==21;
	replace regioncode=22 if statereg43==22;
	replace regioncode=23 if statereg43==23;
	replace regioncode=24 if statereg43==24;
	replace regioncode=41 if statereg43==31;
	replace regioncode=41 if statereg43==32 & dist_code43==10;
	replace regioncode=42 if statereg43==32 & (dist_code43==2 | dist_code43==7 | dist_code43==3 | dist_code43==1);
	replace regioncode=43 if statereg43==33;
	replace regioncode=51 if statereg43==41;
	replace regioncode=52 if statereg43==42;
	replace regioncode=53 if statereg43==43;
	replace regioncode=71 if statereg43==51 & (dist_code43>=17 & dist_code43 <=19);
	replace regioncode=72 if statereg43==51 & dist_code43==9;
	replace regioncode=73 if statereg43==51 & (dist_code43>=14 & dist_code43 <=16);
	replace regioncode=72 if statereg43==52;
	replace regioncode=71 if statereg43==53 & (dist_code43==17 | dist_code43==18);
	replace regioncode=73 if statereg43==53  & (dist_code43>=14 & dist_code43 <=16);
	replace regioncode=72 if statereg43==54 & dist_code==10;
	replace regioncode=74 if statereg43==54 & (dist_code43==7 | dist_code43==8 | dist_code43==3);
	replace regioncode=75 if statereg43==55;
	replace regioncode=81 if statereg43==61;
	replace regioncode=82 if statereg43==62;
	replace regioncode=91 if statereg43==71;
	replace regioncode=101 if statereg43==81;
	replace regioncode=102 if statereg43==82;
	replace regioncode=103 if statereg43==83;
	replace regioncode=111 if statereg43==91;
	replace regioncode=112 if statereg43==92;
	replace regioncode=113 if statereg43==93;
	replace regioncode=114 if statereg43==94;
	replace regioncode=121 if statereg43==101;
	replace regioncode=122 if statereg43==102;
	replace regioncode=131 if statereg43==111;
	replace regioncode=132 if statereg43==112;
	replace regioncode=133 if statereg43==113;
	replace regioncode=134 if statereg43==114;
	replace regioncode=135 if statereg43==115;
	replace regioncode=136 if statereg43==116;
	replace regioncode=137 if statereg43==117;
	replace regioncode=141 if statereg43==121;
	replace regioncode=142 if statereg43==122;
	replace regioncode=143 if statereg43==123;
	replace regioncode=144 if statereg43==124;
	replace regioncode=145 if statereg43==125;
	replace regioncode=146 if statereg43==126;
	replace regioncode=151 if statereg43==131;
	replace regioncode=152 if statereg43==132;
	replace regioncode=161 if statereg43==141;
	replace regioncode=181 if statereg43==151;
	replace regioncode=191 if statereg43==161;
	replace regioncode=192 if statereg43==162;
	replace regioncode=193 if statereg43==163;
	replace regioncode=201 if statereg43==171;
	replace regioncode=202 if statereg43==172;
	replace regioncode=211 if statereg43==181;
	replace regioncode=212 if statereg43==182;
	replace regioncode=213 if statereg43==183;
	replace regioncode=214 if statereg43==184;
	replace regioncode=221 if statereg43==191;
	replace regioncode=231 if statereg43==201;
	replace regioncode=232 if statereg43==202;
	replace regioncode=233 if statereg43==203;
	replace regioncode=234 if statereg43==204;
	replace regioncode=241 if statereg43==211;
	replace regioncode=251 if statereg43==221;
	replace regioncode=252 if statereg43==222;
	replace regioncode=253 if statereg43==223;
	replace regioncode=254 if statereg43==224;
	replace regioncode=255 if statereg43==225;
	replace regioncode=261 if statereg43==231;
	replace regioncode=262 if statereg43==232;
	replace regioncode=263 if statereg43==233;
	replace regioncode=264 if statereg43==234;
	replace regioncode=271 if statereg43==241;
	replace regioncode=31 if statereg43==251;
	replace regioncode=281 if statereg43==261;
	replace regioncode=291 if statereg43==271;
	replace regioncode=311 if statereg43==281;
	replace regioncode=301 if statereg43==291;
	replace regioncode=321 if statereg43==301;
	replace regioncode=171 if statereg43==311;
	replace regioncode=331 if statereg43==321;

	
	*combining first three Gujarat regions into one for consistency;
	replace regioncode=71 if regioncode==72;
	replace regioncode=71 if regioncode==73;

	cap label drop states;
label define states

2	"Andhra Pradesh   "
3	"Assam            "
4	"Bihar            "
5	"Gujarat          "
6	"Haryana          "
7	"Himachal Pradesh "
8	"Jammu &Kashmir   "
9	"Karnataka        "
10	"Kerala           "
11	"Madhya Pradesh   "
12	"Maharashtra      "
13	"Manipur          "
14	"Meghalaya        "
15	"Nagaland         "
16	"Orissa           "
17	"Punjab		  "
18	"Rajasthan	  "	
19	"Sikkim           "
20	"Tamil Nadu	  "
21	"Tripura	  "
22	"Uttar Pradesh	  "
23	"West Bengal	  "
24	"Andaman & Nicobar Islands "
25	"Arunachal Pradesh"
26	"Chandigarh	  "
27	"Dadra &Nagar Haveli"
28	"Delhi            "
29	"Goa, Daman & Diu "
30	"Lakshadweep      "
31	"Mizoram          "
32	"Pondicherry      ";

cap label drop regions;
label define regions 

21	"Andhra Pradesh Costal"
22	"Andhra Pradesh Inland Northern"
23	"Andhra Pradesh South -Western"
24	"Andhra Pradesh Inland Southern"
31	"Arunachal Pradesh"
41	"Assam Plains Western"
42	"Assam Plains Western"
43	"Assam Hills"
51	"Bihar Southern"
52	"Bihar Northern"
53	"Bihar Central"
71	"Gujarat Plains and South-Eastern"
74	"Gujarat Dry areas"
75	"Gujarat Saurashtra"
81	"Haryana Eastern"
82	"Haryana Western"
91	"Himachal Pradesh"
101	"Jammu &Kashmir Mountainous"
102	"Jammu &Kashmir Outer Hills"
103	"Jammu &Kashmir Jhelam Valley"
111	"Karnataka Coastal & Ghats"
112	"Karnataka Inland Eastern"
113	"Karnataka Inland Southern"
114	"Karnataka Inland Nothern"
121	"Kerala Northern"
122	"Kerala Southern"
131	"Madhya Pradesh Chattisgarh"
132	"Madhya Pradesh Vindhya"
133	"Madhya Pradesh Central"
134	"Madhya Pradesh Malwa"
135	"Madhya Pradesh South"
136	"Madhya Pradesh South Western"
137	"Madhya Pradesh Northern"
141	"Maharashtra Coastal"
142	"Maharashtra Inland Western"
143	"Maharashtra Inland Northern"
144	"Maharashtra Inland Central"
145	"Maharashtra Inland Eastern"
146	"Maharashtra Eastern"
151	"Manipur Plains"
152	"Manipur Hills"
161	"Meghalaya"
171	"Mizoram"
181	"Nagaland"
191	"Orissa Coastal"
192	"Orissa Southern"
193	"Orissa Northern"
201	"Punjab Northern"
202	"Punjab Southern"
211	"Rajasthan Western"
212	"Rajasthan North-Eastern"
213	"Rajasthan Southern"
214	"Rajasthan South-Eastern"
221	"Sikkim"
231	"Tamil Nadu Coastal Northern"
232	"Tamil Nadu Coastal"
233	"Tamil Nadu Southern"
234	"Tamil Nadu Inland"
241	"Tripura"
251	"Uttar Pradesh Himalayan"
252	"Uttar Pradesh Western"
253	"Uttar Pradesh Central"
254	"Uttar Pradesh Eastern"
255	"Uttar Pradesh Southern"
261	"West Bengal Himalayan"
262	"West Bengal Eastern Plains"
263	"West Bengal Central Plains"
264	"West Bengal Western Plains"
271	"Andaman & Nicobar Islands"
281	"Chandigarh"
291	"Dadra &Nagar Haveli"
301	"Goa & Daman & Diu"
311	"Delhi"
321	"Lakshadweep"
331	"Pondicherry";

		
	# delimit cr
	
	drop statereg43 dist_code43
	gen statecode=floor(regioncode/10)
	
	
	
	
	save mult43, replace
	
	


use mult55, clear

***some errors in this round-- need to drop because regions do not exist -- only about 15 obs. total
drop if region==55
drop if region==170
drop if region==194
drop if region==203
drop if region==210
drop if region==230
drop if region==240
drop if region==265

gen st_code55=floor(region/10)
gen dist_code55=district
gen regioncode=region

	# delimit ;

	gen statecode=.;
	replace statecode=2 if st_code55==2;
	replace statecode=3 if st_code55==4;
	replace statecode=4 if st_code55==5;
	replace statecode=5 if st_code55==7;
	replace statecode=6 if st_code55==8;
	replace statecode=7 if st_code55==9;
	replace statecode=8 if st_code55==10;
	replace statecode=9 if st_code55==11;
	replace statecode=10 if st_code55==12;
	replace statecode=11 if st_code55==13;
	replace statecode=12 if st_code55==14;
	replace statecode=13 if st_code55==15;
	replace statecode=14 if st_code55==16;
	replace statecode=15 if st_code55==18;
	replace statecode=16 if st_code55==19;
	replace statecode=17 if st_code55==20;
	replace statecode=18 if st_code55==21;
	replace statecode=19 if st_code55==22;
	replace statecode=20 if st_code55==23;
	replace statecode=21 if st_code55==24;
	replace statecode=22 if st_code55==25;
	replace statecode=23 if st_code55==26;
	replace statecode=24 if st_code55==27;
	replace statecode=25 if st_code55==3;
	replace statecode=26 if st_code55==28;
	replace statecode=27 if st_code55==29;
	replace statecode=28 if st_code55==31;
	replace statecode=29 if st_code55==6;
	replace statecode=30 if st_code55==32;
	replace statecode=31 if st_code55==17;
	replace statecode=32 if st_code55==33;
	replace statecode=29 if st_code55==30;

	
	*combining Goa and Daman and Diu into a single region;
	replace regioncode=301 if regioncode==61;

	
	*combining first three Gujarat regions into one for consistency;
	replace regioncode=71 if regioncode==72;
	replace regioncode=71 if regioncode==73;

	gen districtcode=.;

	replace districtcode=1 if st_code55==2 & dist_code55==1;
	replace districtcode=2 if st_code55==2 & dist_code55==2;
	replace districtcode=3 if st_code55==2 & dist_code55==3;
	replace districtcode=4 if st_code55==2 & dist_code55==4;
	replace districtcode=5 if st_code55==2 & dist_code55==5;
	replace districtcode=6 if st_code55==2 & dist_code55==6;
	replace districtcode=7 if st_code55==2 & dist_code55==7;
	replace districtcode=8 if st_code55==2 & dist_code55==8;
	replace districtcode=9 if st_code55==2 & dist_code55==9;
	replace districtcode=10 if st_code55==2 & dist_code55==10;
	replace districtcode=11 if st_code55==2 & dist_code55==11;
	replace districtcode=12 if st_code55==2 & dist_code55==12;
	replace districtcode=13 if st_code55==2 & dist_code55==13;
	replace districtcode=14 if st_code55==2 & dist_code55==14;
	replace districtcode=15 if st_code55==2 & dist_code55==15;
	replace districtcode=16 if st_code55==2 & dist_code55==16;
	replace districtcode=17 if st_code55==2 & dist_code55==17;
	replace districtcode=18 if st_code55==2 & dist_code55==18;
	replace districtcode=19 if st_code55==2 & dist_code55==19;
	replace districtcode=20 if st_code55==2 & dist_code55==20;
	replace districtcode=21 if st_code55==2 & dist_code55==21;
	replace districtcode=22 if st_code55==2 & dist_code55==22;
	replace districtcode=23 if st_code55==2 & dist_code55==23;
	replace districtcode=1 if st_code55==3 & dist_code55==1;
	replace districtcode=1 if st_code55==3 & dist_code55==2;
	replace districtcode=2 if st_code55==3 & dist_code55==3;
	replace districtcode=3 if st_code55==3 & dist_code55==4;
	replace districtcode=4 if st_code55==3 & dist_code55==5;
	replace districtcode=5 if st_code55==3 & dist_code55==6;
	replace districtcode=6 if st_code55==3 & dist_code55==7;
	replace districtcode=7 if st_code55==3 & dist_code55==8;
	replace districtcode=8 if st_code55==3 & dist_code55==9;
	replace districtcode=9 if st_code55==3 & dist_code55==10;
	replace districtcode=9 if st_code55==3 & dist_code55==11;
	replace districtcode=1 if st_code55==4 & dist_code55==1;
	replace districtcode=1 if st_code55==4 & dist_code55==2;
	replace districtcode=1 if st_code55==4 & dist_code55==3;
	replace districtcode=1 if st_code55==4 & dist_code55==4;
	replace districtcode=2 if st_code55==4 & dist_code55==5;
	replace districtcode=2 if st_code55==4 & dist_code55==6;
	replace districtcode=2 if st_code55==4 & dist_code55==7;
	replace districtcode=3 if st_code55==4 & dist_code55==8;
	replace districtcode=3 if st_code55==4 & dist_code55==9;
	replace districtcode=4 if st_code55==4 & dist_code55==10;
	replace districtcode=4 if st_code55==4 & dist_code55==11;
	replace districtcode=7 if st_code55==4 & dist_code55==12;
	replace districtcode=7 if st_code55==4 & dist_code55==13;
	replace districtcode=6 if st_code55==4 & dist_code55==14;
	replace districtcode=6 if st_code55==4 & dist_code55==15;
	replace districtcode=6 if st_code55==4 & dist_code55==16;
	replace districtcode=5 if st_code55==4 & dist_code55==17;
	replace districtcode=5 if st_code55==4 & dist_code55==18;
	replace districtcode=8 if st_code55==4 & dist_code55==19;
	replace districtcode=9 if st_code55==4 & dist_code55==20;
	replace districtcode=10 if st_code55==4 & dist_code55==21;
	replace districtcode=10 if st_code55==4 & dist_code55==22;
	replace districtcode=10 if st_code55==4 & dist_code55==23;
	replace districtcode=1 if st_code55==5 & dist_code55==1;
	replace districtcode=2 if st_code55==5 & dist_code55==2;
	replace districtcode=7 if st_code55==5 & dist_code55==3;
	replace districtcode=6 if st_code55==5 & dist_code55==4;
	replace districtcode=5 if st_code55==5 & dist_code55==5;
	replace districtcode=4 if st_code55==5 & dist_code55==6;
	replace districtcode=4 if st_code55==5 & dist_code55==7;
	replace districtcode=3 if st_code55==5 & dist_code55==8;
	replace districtcode=8 if st_code55==5 & dist_code55==9;
	replace districtcode=9 if st_code55==5 & dist_code55==10;
	replace districtcode=10 if st_code55==5 & dist_code55==11;
	replace districtcode=11 if st_code55==5 & dist_code55==12;
	replace districtcode=12 if st_code55==5 & dist_code55==13;
	replace districtcode=13 if st_code55==5 & dist_code55==14;
	replace districtcode=14 if st_code55==5 & dist_code55==15;
	replace districtcode=15 if st_code55==5 & dist_code55==16;
	replace districtcode=16 if st_code55==5 & dist_code55==17;
	replace districtcode=17 if st_code55==5 & dist_code55==18;
	replace districtcode=18 if st_code55==5 & dist_code55==19;
	replace districtcode=19 if st_code55==5 & dist_code55==20;
	replace districtcode=20 if st_code55==5 & dist_code55==21;
	replace districtcode=32 if st_code55==5 & dist_code55==22;
	replace districtcode=21 if st_code55==5 & dist_code55==23;
	replace districtcode=22 if st_code55==5 & dist_code55==24;
	replace districtcode=33 if st_code55==5 & dist_code55==25;
	replace districtcode=23 if st_code55==5 & dist_code55==26;
	replace districtcode=24 if st_code55==5 & dist_code55==27;
	replace districtcode=35 if st_code55==5 & dist_code55==28;
	replace districtcode=36 if st_code55==5 & dist_code55==29;
	replace districtcode=25 if st_code55==5 & dist_code55==30;
	replace districtcode=34 if st_code55==5 & dist_code55==31;
	replace districtcode=26 if st_code55==5 & dist_code55==32;
	replace districtcode=27 if st_code55==5 & dist_code55==33;
	replace districtcode=28 if st_code55==5 & dist_code55==34;
	replace districtcode=29 if st_code55==5 & dist_code55==35;
	replace districtcode=38 if st_code55==5 & dist_code55==36;
	replace districtcode=37 if st_code55==5 & dist_code55==37;
	replace districtcode=30 if st_code55==5 & dist_code55==38;
	replace districtcode=31 if st_code55==5 & dist_code55==39;
	replace districtcode=31 if st_code55==5 & dist_code55==40;
	replace districtcode=21 if st_code55==5 & dist_code55==41;
	replace districtcode=21 if st_code55==5 & dist_code55==42;
	replace districtcode=7 if st_code55==5 & dist_code55==43;
	replace districtcode=6 if st_code55==5 & dist_code55==44;
	replace districtcode=20 if st_code55==5 & dist_code55==45;
	replace districtcode=23 if st_code55==5 & dist_code55==46;
	replace districtcode=24 if st_code55==5 & dist_code55==47;
	replace districtcode=36 if st_code55==5 & dist_code55==48;
	replace districtcode=27 if st_code55==5 & dist_code55==49;
	replace districtcode=28 if st_code55==5 & dist_code55==50;
	replace districtcode=28 if st_code55==5 & dist_code55==51;
	replace districtcode=29 if st_code55==5 & dist_code55==52;
	replace districtcode=1 if st_code55==6 & dist_code55==1;
	replace districtcode=1 if st_code55==6 & dist_code55==2;
	replace districtcode=1 if st_code55==7 & dist_code55==1;
	replace districtcode=2 if st_code55==7 & dist_code55==2;
	replace districtcode=3 if st_code55==7 & dist_code55==3;
	replace districtcode=4 if st_code55==7 & dist_code55==4;
	replace districtcode=5 if st_code55==7 & dist_code55==5;
	replace districtcode=6 if st_code55==7 & dist_code55==6;
	replace districtcode=7 if st_code55==7 & dist_code55==7;
	replace districtcode=8 if st_code55==7 & dist_code55==8;
	replace districtcode=9 if st_code55==7 & dist_code55==9;
	replace districtcode=10 if st_code55==7 & dist_code55==10;
	replace districtcode=11 if st_code55==7 & dist_code55==11;
	replace districtcode=12 if st_code55==7 & dist_code55==12;
	replace districtcode=13 if st_code55==7 & dist_code55==13;
	replace districtcode=14 if st_code55==7 & dist_code55==14;
	replace districtcode=15 if st_code55==7 & dist_code55==15;
	replace districtcode=16 if st_code55==7 & dist_code55==16;
	replace districtcode=17 if st_code55==7 & dist_code55==17;
	replace districtcode=18 if st_code55==7 & dist_code55==18;
	replace districtcode=19 if st_code55==7 & dist_code55==19;
	replace districtcode=1 if st_code55==8 & dist_code55==1;
	replace districtcode=1 if st_code55==8 & dist_code55==2;
	replace districtcode=2 if st_code55==8 & dist_code55==3;
	replace districtcode=2 if st_code55==8 & dist_code55==4;
	replace districtcode=3 if st_code55==8 & dist_code55==5;
	replace districtcode=3 if st_code55==8 & dist_code55==6;
	replace districtcode=5 if st_code55==8 & dist_code55==7;
	replace districtcode=6 if st_code55==8 & dist_code55==8;
	replace districtcode=7 if st_code55==8 & dist_code55==9;
	replace districtcode=8 if st_code55==8 & dist_code55==10;
	replace districtcode=9 if st_code55==8 & dist_code55==11;
	replace districtcode=9 if st_code55==8 & dist_code55==12;
	replace districtcode=10 if st_code55==8 & dist_code55==13;
	replace districtcode=4 if st_code55==8 & dist_code55==14;
	replace districtcode=11 if st_code55==8 & dist_code55==15;
	replace districtcode=12 if st_code55==8 & dist_code55==16;
	replace districtcode=1 if st_code55==9 & dist_code55==1;
	replace districtcode=2 if st_code55==9 & dist_code55==2;
	replace districtcode=3 if st_code55==9 & dist_code55==3;
	replace districtcode=4 if st_code55==9 & dist_code55==4;
	replace districtcode=5 if st_code55==9 & dist_code55==5;
	replace districtcode=6 if st_code55==9 & dist_code55==6;
	replace districtcode=7 if st_code55==9 & dist_code55==7;
	replace districtcode=8 if st_code55==9 & dist_code55==8;
	replace districtcode=9 if st_code55==9 & dist_code55==9;
	replace districtcode=10 if st_code55==9 & dist_code55==10;
	replace districtcode=11 if st_code55==9 & dist_code55==11;
	replace districtcode=12 if st_code55==9 & dist_code55==12;
	replace districtcode=1 if st_code55==10 & dist_code55==1;
	replace districtcode=2 if st_code55==10 & dist_code55==2;
	replace districtcode=3 if st_code55==10 & dist_code55==3;
	replace districtcode=4 if st_code55==10 & dist_code55==4;
	replace districtcode=5 if st_code55==10 & dist_code55==5;
	replace districtcode=6 if st_code55==10 & dist_code55==6;
	replace districtcode=7 if st_code55==10 & dist_code55==7;
	replace districtcode=8 if st_code55==10 & dist_code55==8;
	replace districtcode=9 if st_code55==10 & dist_code55==9;
	replace districtcode=10 if st_code55==10 & dist_code55==10;
	replace districtcode=11 if st_code55==10 & dist_code55==11;
	replace districtcode=12 if st_code55==10 & dist_code55==12;
	replace districtcode=13 if st_code55==10 & dist_code55==13;
	replace districtcode=14 if st_code55==10 & dist_code55==14;
	replace districtcode=1 if st_code55==11 & dist_code55==1;
	replace districtcode=1 if st_code55==11 & dist_code55==2;
	replace districtcode=2 if st_code55==11 & dist_code55==3;
	replace districtcode=3 if st_code55==11 & dist_code55==4;
	replace districtcode=4 if st_code55==11 & dist_code55==5;
	replace districtcode=5 if st_code55==11 & dist_code55==6;
	replace districtcode=6 if st_code55==11 & dist_code55==7;
	replace districtcode=7 if st_code55==11 & dist_code55==8;
	replace districtcode=8 if st_code55==11 & dist_code55==9;
	replace districtcode=9 if st_code55==11 & dist_code55==10;
	replace districtcode=10 if st_code55==11 & dist_code55==11;
	replace districtcode=11 if st_code55==11 & dist_code55==12;
	replace districtcode=12 if st_code55==11 & dist_code55==13;
	replace districtcode=13 if st_code55==11 & dist_code55==14;
	replace districtcode=14 if st_code55==11 & dist_code55==15;
	replace districtcode=15 if st_code55==11 & dist_code55==16;
	replace districtcode=16 if st_code55==11 & dist_code55==17;
	replace districtcode=17 if st_code55==11 & dist_code55==18;
	replace districtcode=18 if st_code55==11 & dist_code55==19;
	replace districtcode=19 if st_code55==11 & dist_code55==20;
	replace districtcode=13 if st_code55==12 & dist_code55==1;
	replace districtcode=1 if st_code55==12 & dist_code55==2;
	replace districtcode=2 if st_code55==12 & dist_code55==3;
	replace districtcode=3 if st_code55==12 & dist_code55==4;
	replace districtcode=4 if st_code55==12 & dist_code55==5;
	replace districtcode=5 if st_code55==12 & dist_code55==6;
	replace districtcode=6 if st_code55==12 & dist_code55==7;
	replace districtcode=7 if st_code55==12 & dist_code55==8;
	replace districtcode=8 if st_code55==12 & dist_code55==9;
	replace districtcode=9 if st_code55==12 & dist_code55==10;
	replace districtcode=10 if st_code55==12 & dist_code55==11;
	replace districtcode=14 if st_code55==12 & dist_code55==12;
	replace districtcode=11 if st_code55==12 & dist_code55==13;
	replace districtcode=12 if st_code55==12 & dist_code55==14;
	replace districtcode=1 if st_code55==13 & dist_code55==1;
	replace districtcode=2 if st_code55==13 & dist_code55==2;
	replace districtcode=3 if st_code55==13 & dist_code55==3;
	replace districtcode=4 if st_code55==13 & dist_code55==4;
	replace districtcode=5 if st_code55==13 & dist_code55==5;
	replace districtcode=6 if st_code55==13 & dist_code55==6;
	replace districtcode=7 if st_code55==13 & dist_code55==7;
	replace districtcode=8 if st_code55==13 & dist_code55==8;
	replace districtcode=9 if st_code55==13 & dist_code55==9;
	replace districtcode=10 if st_code55==13 & dist_code55==10;
	replace districtcode=11 if st_code55==13 & dist_code55==11;
	replace districtcode=12 if st_code55==13 & dist_code55==12;
	replace districtcode=13 if st_code55==13 & dist_code55==13;
	replace districtcode=14 if st_code55==13 & dist_code55==14;
	replace districtcode=15 if st_code55==13 & dist_code55==15;
	replace districtcode=16 if st_code55==13 & dist_code55==16;
	replace districtcode=17 if st_code55==13 & dist_code55==17;
	replace districtcode=18 if st_code55==13 & dist_code55==18;
	replace districtcode=19 if st_code55==13 & dist_code55==19;
	replace districtcode=20 if st_code55==13 & dist_code55==20;
	replace districtcode=21 if st_code55==13 & dist_code55==21;
	replace districtcode=22 if st_code55==13 & dist_code55==22;
	replace districtcode=23 if st_code55==13 & dist_code55==23;
	replace districtcode=24 if st_code55==13 & dist_code55==24;
	replace districtcode=25 if st_code55==13 & dist_code55==25;
	replace districtcode=26 if st_code55==13 & dist_code55==26;
	replace districtcode=27 if st_code55==13 & dist_code55==27;
	replace districtcode=28 if st_code55==13 & dist_code55==28;
	replace districtcode=29 if st_code55==13 & dist_code55==29;
	replace districtcode=30 if st_code55==13 & dist_code55==30;
	replace districtcode=31 if st_code55==13 & dist_code55==31;
	replace districtcode=32 if st_code55==13 & dist_code55==32;
	replace districtcode=33 if st_code55==13 & dist_code55==33;
	replace districtcode=34 if st_code55==13 & dist_code55==34;
	replace districtcode=35 if st_code55==13 & dist_code55==35;
	replace districtcode=36 if st_code55==13 & dist_code55==36;
	replace districtcode=37 if st_code55==13 & dist_code55==37;
	replace districtcode=38 if st_code55==13 & dist_code55==38;
	replace districtcode=39 if st_code55==13 & dist_code55==39;
	replace districtcode=40 if st_code55==13 & dist_code55==40;
	replace districtcode=41 if st_code55==13 & dist_code55==41;
	replace districtcode=42 if st_code55==13 & dist_code55==42;
	replace districtcode=43 if st_code55==13 & dist_code55==43;
	replace districtcode=44 if st_code55==13 & dist_code55==44;
	replace districtcode=45 if st_code55==13 & dist_code55==45;
	replace districtcode=1 if st_code55==14 & dist_code55==1;
	replace districtcode=2 if st_code55==14 & dist_code55==2;
	replace districtcode=3 if st_code55==14 & dist_code55==3;
	replace districtcode=4 if st_code55==14 & dist_code55==4;
	replace districtcode=28 if st_code55==14 & dist_code55==5;
	replace districtcode=5 if st_code55==14 & dist_code55==6;
	replace districtcode=6 if st_code55==14 & dist_code55==7;
	replace districtcode=7 if st_code55==14 & dist_code55==8;
	replace districtcode=8 if st_code55==14 & dist_code55==9;
	replace districtcode=9 if st_code55==14 & dist_code55==10;
	replace districtcode=10 if st_code55==14 & dist_code55==11;
	replace districtcode=11 if st_code55==14 & dist_code55==12;
	replace districtcode=12 if st_code55==14 & dist_code55==13;
	replace districtcode=13 if st_code55==14 & dist_code55==14;
	replace districtcode=14 if st_code55==14 & dist_code55==15;
	replace districtcode=27 if st_code55==14 & dist_code55==16;
	replace districtcode=15 if st_code55==14 & dist_code55==17;
	replace districtcode=16 if st_code55==14 & dist_code55==18;
	replace districtcode=17 if st_code55==14 & dist_code55==19;
	replace districtcode=18 if st_code55==14 & dist_code55==20;
	replace districtcode=29 if st_code55==14 & dist_code55==21;
	replace districtcode=19 if st_code55==14 & dist_code55==22;
	replace districtcode=20 if st_code55==14 & dist_code55==23;
	replace districtcode=21 if st_code55==14 & dist_code55==24;
	replace districtcode=22 if st_code55==14 & dist_code55==25;
	replace districtcode=23 if st_code55==14 & dist_code55==26;
	replace districtcode=24 if st_code55==14 & dist_code55==27;
	replace districtcode=25 if st_code55==14 & dist_code55==28;
	replace districtcode=26 if st_code55==14 & dist_code55==29;
	replace districtcode=26 if st_code55==14 & dist_code55==30;
	replace districtcode=1 if st_code55==15 & dist_code55==1;
	replace districtcode=2 if st_code55==15 & dist_code55==2;
	replace districtcode=3 if st_code55==15 & dist_code55==3;
	replace districtcode=4 if st_code55==15 & dist_code55==4;
	replace districtcode=7 if st_code55==15 & dist_code55==5;
	replace districtcode=6 if st_code55==15 & dist_code55==6;
	replace districtcode=5 if st_code55==15 & dist_code55==7;
	replace districtcode=8 if st_code55==15 & dist_code55==8;
	replace districtcode=1 if st_code55==16 & dist_code55==1;
	replace districtcode=2 if st_code55==16 & dist_code55==2;
	replace districtcode=3 if st_code55==16 & dist_code55==3;
	replace districtcode=4 if st_code55==16 & dist_code55==4;
	replace districtcode=5 if st_code55==16 & dist_code55==5;
	replace districtcode=5 if st_code55==16 & dist_code55==6;
	replace districtcode=2 if st_code55==16 & dist_code55==7;
	replace districtcode=1 if st_code55==17 & dist_code55==1;
	replace districtcode=2 if st_code55==17 & dist_code55==2;
	replace districtcode=3 if st_code55==17 & dist_code55==3;
	replace districtcode=1 if st_code55==18 & dist_code55==1;
	replace districtcode=2 if st_code55==18 & dist_code55==2;
	replace districtcode=4 if st_code55==18 & dist_code55==3;
	replace districtcode=3 if st_code55==18 & dist_code55==4;
	replace districtcode=5 if st_code55==18 & dist_code55==5;
	replace districtcode=6 if st_code55==18 & dist_code55==6;
	replace districtcode=7 if st_code55==18 & dist_code55==7;
	replace districtcode=1 if st_code55==19 & dist_code55==1;
	replace districtcode=2 if st_code55==19 & dist_code55==2;
	replace districtcode=3 if st_code55==19 & dist_code55==3;
	replace districtcode=4 if st_code55==19 & dist_code55==4;
	replace districtcode=5 if st_code55==19 & dist_code55==5;
	replace districtcode=6 if st_code55==19 & dist_code55==6;
	replace districtcode=7 if st_code55==19 & dist_code55==7;
	replace districtcode=8 if st_code55==19 & dist_code55==8;
	replace districtcode=9 if st_code55==19 & dist_code55==9;
	replace districtcode=10 if st_code55==19 & dist_code55==10;
	replace districtcode=11 if st_code55==19 & dist_code55==11;
	replace districtcode=12 if st_code55==19 & dist_code55==12;
	replace districtcode=13 if st_code55==19 & dist_code55==13;
	replace districtcode=5 if st_code55==19 & dist_code55==14;
	replace districtcode=6 if st_code55==19 & dist_code55==15;
	replace districtcode=6 if st_code55==19 & dist_code55==16;
	replace districtcode=6 if st_code55==19 & dist_code55==17;
	replace districtcode=13 if st_code55==19 & dist_code55==18;
	replace districtcode=13 if st_code55==19 & dist_code55==19;
	replace districtcode=12 if st_code55==19 & dist_code55==20;
	replace districtcode=8 if st_code55==19 & dist_code55==21;
	replace districtcode=10 if st_code55==19 & dist_code55==22;
	replace districtcode=11 if st_code55==19 & dist_code55==23;
	replace districtcode=11 if st_code55==19 & dist_code55==24;
	replace districtcode=11 if st_code55==19 & dist_code55==25;
	replace districtcode=1 if st_code55==19 & dist_code55==26;
	replace districtcode=1 if st_code55==19 & dist_code55==27;
	replace districtcode=1 if st_code55==19 & dist_code55==28;
	replace districtcode=9 if st_code55==19 & dist_code55==29;
	replace districtcode=7 if st_code55==19 & dist_code55==30;
	replace districtcode=1 if st_code55==20 & dist_code55==1;
	replace districtcode=2 if st_code55==20 & dist_code55==2;
	replace districtcode=3 if st_code55==20 & dist_code55==3;
	replace districtcode=4 if st_code55==20 & dist_code55==4;
	replace districtcode=5 if st_code55==20 & dist_code55==5;
	replace districtcode=6 if st_code55==20 & dist_code55==6;
	replace districtcode=7 if st_code55==20 & dist_code55==7;
	replace districtcode=8 if st_code55==20 & dist_code55==8;
	replace districtcode=9 if st_code55==20 & dist_code55==9;
	replace districtcode=10 if st_code55==20 & dist_code55==10;
	replace districtcode=11 if st_code55==20 & dist_code55==11;
	replace districtcode=12 if st_code55==20 & dist_code55==12;
	replace districtcode=12 if st_code55==20 & dist_code55==13;
	replace districtcode=11 if st_code55==20 & dist_code55==14;
	replace districtcode=1 if st_code55==21 & dist_code55==1;
	replace districtcode=2 if st_code55==21 & dist_code55==2;
	replace districtcode=3 if st_code55==21 & dist_code55==3;
	replace districtcode=4 if st_code55==21 & dist_code55==4;
	replace districtcode=5 if st_code55==21 & dist_code55==5;
	replace districtcode=6 if st_code55==21 & dist_code55==6;
	replace districtcode=27 if st_code55==21 & dist_code55==7;
	replace districtcode=7 if st_code55==21 & dist_code55==8;
	replace districtcode=8 if st_code55==21 & dist_code55==9;
	replace districtcode=9 if st_code55==21 & dist_code55==10;
	replace districtcode=10 if st_code55==21 & dist_code55==11;
	replace districtcode=11 if st_code55==21 & dist_code55==12;
	replace districtcode=12 if st_code55==21 & dist_code55==13;
	replace districtcode=13 if st_code55==21 & dist_code55==14;
	replace districtcode=14 if st_code55==21 & dist_code55==15;
	replace districtcode=15 if st_code55==21 & dist_code55==16;
	replace districtcode=16 if st_code55==21 & dist_code55==17;
	replace districtcode=17 if st_code55==21 & dist_code55==18;
	replace districtcode=18 if st_code55==21 & dist_code55==19;
	replace districtcode=19 if st_code55==21 & dist_code55==20;
	replace districtcode=20 if st_code55==21 & dist_code55==21;
	replace districtcode=21 if st_code55==21 & dist_code55==22;
	replace districtcode=22 if st_code55==21 & dist_code55==23;
	replace districtcode=23 if st_code55==21 & dist_code55==24;
	replace districtcode=24 if st_code55==21 & dist_code55==25;
	replace districtcode=25 if st_code55==21 & dist_code55==26;
	replace districtcode=26 if st_code55==21 & dist_code55==27;
	replace districtcode=8 if st_code55==21 & dist_code55==28;
	replace districtcode=25 if st_code55==21 & dist_code55==29;
	replace districtcode=20 if st_code55==21 & dist_code55==30;
	replace districtcode=1 if st_code55==22 & dist_code55==1;
	replace districtcode=2 if st_code55==22 & dist_code55==2;
	replace districtcode=3 if st_code55==22 & dist_code55==3;
	replace districtcode=4 if st_code55==22 & dist_code55==4;
	replace districtcode=1 if st_code55==23 & dist_code55==1;
	replace districtcode=2 if st_code55==23 & dist_code55==2;
	replace districtcode=3 if st_code55==23 & dist_code55==3;
	replace districtcode=5 if st_code55==23 & dist_code55==4;
	replace districtcode=3 if st_code55==23 & dist_code55==5;
	replace districtcode=4 if st_code55==23 & dist_code55==6;
	replace districtcode=6 if st_code55==23 & dist_code55==7;
	replace districtcode=7 if st_code55==23 & dist_code55==8;
	replace districtcode=9 if st_code55==23 & dist_code55==9;
	replace districtcode=8 if st_code55==23 & dist_code55==10;
	replace districtcode=19 if st_code55==23 & dist_code55==11;
	replace districtcode=11 if st_code55==23 & dist_code55==12;
	replace districtcode=12 if st_code55==23 & dist_code55==13;
	replace districtcode=13 if st_code55==23 & dist_code55==14;
	replace districtcode=17 if st_code55==23 & dist_code55==15;
	replace districtcode=10 if st_code55==23 & dist_code55==16;
	replace districtcode=14 if st_code55==23 & dist_code55==17;
	replace districtcode=18 if st_code55==23 & dist_code55==18;
	replace districtcode=15 if st_code55==23 & dist_code55==19;
	replace districtcode=15 if st_code55==23 & dist_code55==20;
	replace districtcode=16 if st_code55==23 & dist_code55==21;
	replace districtcode=4 if st_code55==23 & dist_code55==22;
	replace districtcode=12 if st_code55==23 & dist_code55==23;
	replace districtcode=1 if st_code55==24 & dist_code55==1;
	replace districtcode=2 if st_code55==24 & dist_code55==2;
	replace districtcode=3 if st_code55==24 & dist_code55==3;
	replace districtcode=1 if st_code55==25 & dist_code55==1;
	replace districtcode=2 if st_code55==25 & dist_code55==2;
	replace districtcode=3 if st_code55==25 & dist_code55==3;
	replace districtcode=4 if st_code55==25 & dist_code55==4;
	replace districtcode=5 if st_code55==25 & dist_code55==5;
	replace districtcode=6 if st_code55==25 & dist_code55==6;
	replace districtcode=7 if st_code55==25 & dist_code55==7;
	replace districtcode=8 if st_code55==25 & dist_code55==8;
	replace districtcode=11 if st_code55==25 & dist_code55==9;
	replace districtcode=15 if st_code55==25 & dist_code55==10;
	replace districtcode=16 if st_code55==25 & dist_code55==11;
	replace districtcode=9 if st_code55==25 & dist_code55==12;
	replace districtcode=9 if st_code55==25 & dist_code55==13;
	replace districtcode=10 if st_code55==25 & dist_code55==14;
	replace districtcode=12 if st_code55==25 & dist_code55==15;
	replace districtcode=13 if st_code55==25 & dist_code55==16;
	replace districtcode=14 if st_code55==25 & dist_code55==17;
	replace districtcode=21 if st_code55==25 & dist_code55==18;
	replace districtcode=22 if st_code55==25 & dist_code55==19;
	replace districtcode=23 if st_code55==25 & dist_code55==20;
	replace districtcode=25 if st_code55==25 & dist_code55==21;
	replace districtcode=24 if st_code55==25 & dist_code55==22;
	replace districtcode=25 if st_code55==25 & dist_code55==23;
	replace districtcode=17 if st_code55==25 & dist_code55==24;
	replace districtcode=18 if st_code55==25 & dist_code55==25;
	replace districtcode=19 if st_code55==25 & dist_code55==26;
	replace districtcode=20 if st_code55==25 & dist_code55==27;
	replace districtcode=36 if st_code55==25 & dist_code55==28;
	replace districtcode=37 if st_code55==25 & dist_code55==29;
	replace districtcode=38 if st_code55==25 & dist_code55==30;
	replace districtcode=39 if st_code55==25 & dist_code55==31;
	replace districtcode=40 if st_code55==25 & dist_code55==32;
	replace districtcode=41 if st_code55==25 & dist_code55==33;
	replace districtcode=26 if st_code55==25 & dist_code55==34;
	replace districtcode=27 if st_code55==25 & dist_code55==35;
	replace districtcode=28 if st_code55==25 & dist_code55==36;
	replace districtcode=28 if st_code55==25 & dist_code55==37;
	replace districtcode=31 if st_code55==25 & dist_code55==38;
	replace districtcode=32 if st_code55==25 & dist_code55==39;
	replace districtcode=33 if st_code55==25 & dist_code55==40;
	replace districtcode=34 if st_code55==25 & dist_code55==41;
	replace districtcode=35 if st_code55==25 & dist_code55==42;
	replace districtcode=29 if st_code55==25 & dist_code55==43;
	replace districtcode=47 if st_code55==25 & dist_code55==44;
	replace districtcode=30 if st_code55==25 & dist_code55==45;
	replace districtcode=42 if st_code55==25 & dist_code55==46;
	replace districtcode=43 if st_code55==25 & dist_code55==47;
	replace districtcode=44 if st_code55==25 & dist_code55==48;
	replace districtcode=45 if st_code55==25 & dist_code55==49;
	replace districtcode=46 if st_code55==25 & dist_code55==50;
	replace districtcode=48 if st_code55==25 & dist_code55==51;
	replace districtcode=49 if st_code55==25 & dist_code55==52;
	replace districtcode=48 if st_code55==25 & dist_code55==53;
	replace districtcode=49 if st_code55==25 & dist_code55==54;
	replace districtcode=50 if st_code55==25 & dist_code55==55;
	replace districtcode=51 if st_code55==25 & dist_code55==56;
	replace districtcode=51 if st_code55==25 & dist_code55==57;
	replace districtcode=52 if st_code55==25 & dist_code55==58;
	replace districtcode=53 if st_code55==25 & dist_code55==59;
	replace districtcode=54 if st_code55==25 & dist_code55==60;
	replace districtcode=55 if st_code55==25 & dist_code55==61;
	replace districtcode=56 if st_code55==25 & dist_code55==62;
	replace districtcode=56 if st_code55==25 & dist_code55==63;
	replace districtcode=35 if st_code55==25 & dist_code55==69;
	replace districtcode=1 if st_code55==26 & dist_code55==1;
	replace districtcode=2 if st_code55==26 & dist_code55==2;
	replace districtcode=3 if st_code55==26 & dist_code55==3;
	replace districtcode=4 if st_code55==26 & dist_code55==4;
	replace districtcode=5 if st_code55==26 & dist_code55==5;
	replace districtcode=6 if st_code55==26 & dist_code55==6;
	replace districtcode=7 if st_code55==26 & dist_code55==7;
	replace districtcode=8 if st_code55==26 & dist_code55==8;
	replace districtcode=17 if st_code55==26 & dist_code55==9;
	replace districtcode=9 if st_code55==26 & dist_code55==10;
	replace districtcode=10 if st_code55==26 & dist_code55==11;
	replace districtcode=11 if st_code55==26 & dist_code55==12;
	replace districtcode=12 if st_code55==26 & dist_code55==13;
	replace districtcode=13 if st_code55==26 & dist_code55==14;
	replace districtcode=14 if st_code55==26 & dist_code55==15;
	replace districtcode=15 if st_code55==26 & dist_code55==16;
	replace districtcode=16 if st_code55==26 & dist_code55==17;
	replace districtcode=1 if st_code55==27 & dist_code55==1;
	replace districtcode=2 if st_code55==27 & dist_code55==2;
	replace districtcode=1 if st_code55==28 & dist_code55==1;
	replace districtcode=1 if st_code55==29 & dist_code55==1;
	replace districtcode=2 if st_code55==30 & dist_code55==1;
	replace districtcode=3 if st_code55==30 & dist_code55==2;
	replace districtcode=1 if st_code55==31 & dist_code55==1;
	replace districtcode=1 if st_code55==32 & dist_code55==1;
	replace districtcode=1 if st_code55==33 & dist_code55==1;
	replace districtcode=2 if st_code55==33 & dist_code55==2;
	replace districtcode=3 if st_code55==33 & dist_code55==3;
	replace districtcode=4 if st_code55==33 & dist_code55==4;

	cap label drop states;
label define states

2	"Andhra Pradesh   "
3	"Assam            "
4	"Bihar            "
5	"Gujarat          "
6	"Haryana          "
7	"Himachal Pradesh "
8	"Jammu &Kashmir   "
9	"Karnataka        "
10	"Kerala           "
11	"Madhya Pradesh   "
12	"Maharashtra      "
13	"Manipur          "
14	"Meghalaya        "
15	"Nagaland         "
16	"Orissa           "
17	"Punjab		  "
18	"Rajasthan	  "	
19	"Sikkim           "
20	"Tamil Nadu	  "
21	"Tripura	  "
22	"Uttar Pradesh	  "
23	"West Bengal	  "
24	"Andaman & Nicobar Islands "
25	"Arunachal Pradesh"
26	"Chandigarh	  "
27	"Dadra &Nagar Haveli"
28	"Delhi            "
29	"Goa, Daman & Diu "
30	"Lakshadweep      "
31	"Mizoram          "
32	"Pondicherry      ";

cap label drop regions;
label define regions 

21	"Andhra Pradesh Costal"
22	"Andhra Pradesh Inland Northern"
23	"Andhra Pradesh South -Western"
24	"Andhra Pradesh Inland Southern"
31	"Arunachal Pradesh"
41	"Assam Plains Western"
42	"Assam Plains Western"
43	"Assam Hills"
51	"Bihar Southern"
52	"Bihar Northern"
53	"Bihar Central"
71	"Gujarat Plains and South-Eastern"
74	"Gujarat Dry areas"
75	"Gujarat Saurashtra"
81	"Haryana Eastern"
82	"Haryana Western"
91	"Himachal Pradesh"
101	"Jammu &Kashmir Mountainous"
102	"Jammu &Kashmir Outer Hills"
103	"Jammu &Kashmir Jhelam Valley"
111	"Karnataka Coastal & Ghats"
112	"Karnataka Inland Eastern"
113	"Karnataka Inland Southern"
114	"Karnataka Inland Nothern"
121	"Kerala Northern"
122	"Kerala Southern"
131	"Madhya Pradesh Chattisgarh"
132	"Madhya Pradesh Vindhya"
133	"Madhya Pradesh Central"
134	"Madhya Pradesh Malwa"
135	"Madhya Pradesh South"
136	"Madhya Pradesh South Western"
137	"Madhya Pradesh Northern"
141	"Maharashtra Coastal"
142	"Maharashtra Inland Western"
143	"Maharashtra Inland Northern"
144	"Maharashtra Inland Central"
145	"Maharashtra Inland Eastern"
146	"Maharashtra Eastern"
151	"Manipur Plains"
152	"Manipur Hills"
161	"Meghalaya"
171	"Mizoram"
181	"Nagaland"
191	"Orissa Coastal"
192	"Orissa Southern"
193	"Orissa Northern"
201	"Punjab Northern"
202	"Punjab Southern"
211	"Rajasthan Western"
212	"Rajasthan North-Eastern"
213	"Rajasthan Southern"
214	"Rajasthan South-Eastern"
221	"Sikkim"
231	"Tamil Nadu Coastal Northern"
232	"Tamil Nadu Coastal"
233	"Tamil Nadu Southern"
234	"Tamil Nadu Inland"
241	"Tripura"
251	"Uttar Pradesh Himalayan"
252	"Uttar Pradesh Western"
253	"Uttar Pradesh Central"
254	"Uttar Pradesh Eastern"
255	"Uttar Pradesh Southern"
261	"West Bengal Himalayan"
262	"West Bengal Eastern Plains"
263	"West Bengal Central Plains"
264	"West Bengal Western Plains"
271	"Andaman & Nicobar Islands"
281	"Chandigarh"
291	"Dadra &Nagar Haveli"
301	"Goa & Daman & Diu"
311	"Delhi"
321	"Lakshadweep"
331	"Pondicherry";

	# delimit cr
	
	
drop dist_code55 st_code55
save mult55, replace

	
	
use mult61, clear

# delimit ;

*need to first convert states from 61 to 55;
gen statereg61=region;
	gen st_code61=floor(statereg61/10);
	gen st_code55=.;
	replace st_code55=2 if st_code61==28;
	replace st_code55=3 if st_code61==12;
	replace st_code55=4 if st_code61==18;
	replace st_code55=5 if st_code61==10;
	replace st_code55=6 if st_code61==30;
	replace st_code55=7 if st_code61==24;
	replace st_code55=8 if st_code61==6;
	replace st_code55=9 if st_code61==2;
	replace st_code55=10 if st_code61==1;
	replace st_code55=11 if st_code61==29;
	replace st_code55=12 if st_code61==32;
	replace st_code55=13 if st_code61==23;
	replace st_code55=14 if st_code61==27;
	replace st_code55=15 if st_code61==14;
	replace st_code55=16 if st_code61==17;
	replace st_code55=17 if st_code61==15;
	replace st_code55=18 if st_code61==13;
	replace st_code55=19 if st_code61==21;
	replace st_code55=20 if st_code61==3;
	replace st_code55=21 if st_code61==8;
	replace st_code55=22 if st_code61==11;
	replace st_code55=23 if st_code61==33;
	replace st_code55=24 if st_code61==16;
	replace st_code55=25 if st_code61==9;
	replace st_code55=26 if st_code61==19;
	replace st_code55=27 if st_code61==35;
	replace st_code55=28 if st_code61==4;
	replace st_code55=29 if st_code61==26;
	replace st_code55=30 if st_code61==25;
	replace st_code55=31 if st_code61==7;
	replace st_code55=32 if st_code61==31;
	replace st_code55=33 if st_code61==34;
	*Chattisgarh used to be part of Madhya Pradesh;
	replace st_code55=13 if st_code61==22;
	*Uttaranchal used to be part of Uttar Pradesh;
	replace st_code55=25 if st_code61==5;
	*Jharkand used to be part of Bihar;
	replace st_code55=5 if st_code61==20;
	
	
	

	
	
	
	*Now convert regions between 61st and 55th rounds;
	gen reg61=statereg61-(10*st_code61);
	gen reg55=reg61;
	*Jharkand split off but was region 1 of Bihar in 55th round;
	replace reg55=2 if reg61==1 & st_code61==10;
	replace reg55=3 if reg61==2 & st_code61==10;
	replace reg55=1 if reg61==1 & st_code61==20;
	
	*Chattisgarh split off but was region 1 of Madhya Pradesh in 55th round;
	replace reg55=1 if reg61==1 & st_code61==4;
	replace reg55=2 if reg61==1 & st_code61==23;
	replace reg55=3 if reg61==2 & st_code61==23;
	replace reg55=4 if reg61==3 & st_code61==23;
	replace reg55=5 if reg61==4 & st_code61==23;
	replace reg55=6 if reg61==5 & st_code61==23;
	replace reg55=7 if reg61==6 & st_code61==23;
	
	*Uttaranchal split off but was region of Uttar Pradesh in 55th round;
	replace reg55=1 if reg61==1 & st_code61==5;
	replace reg55=2 if reg61==1 & st_code61==9;
	replace reg55=3 if reg61==2 & st_code61==9;
	replace reg55=4 if reg61==3 & st_code61==9;
	replace reg55=5 if reg61==4 & st_code61==9;
	
	
	gen statereg55=(st_code55*10)+reg55;

	
	***Need to do district adjustments for certain regions?;
	
	gen regioncode=statereg55;
	*Gujarat - this needs minor fixing;
	gen dist_code61=district;
	
	
	*combining first three Gujarat regions into one for consistency;
	replace regioncode=71 if regioncode==72;
	replace regioncode=71 if regioncode==73;
		
	*Uttar Pradesh- no need to recode, seems to be consistent already;
    *Goa and Daman and Diu are combined in a region in earlier rounds?;
	replace regioncode=301 if regioncode==61;
	
	*now convert from 55th to earlier state codes;
	gen statecode=.;
	replace statecode=2 if st_code55==2;
	replace statecode=3 if st_code55==4;
	replace statecode=4 if st_code55==5;
	replace statecode=5 if st_code55==7;
	replace statecode=6 if st_code55==8;
	replace statecode=7 if st_code55==9;
	replace statecode=8 if st_code55==10;
	replace statecode=9 if st_code55==11;
	replace statecode=10 if st_code55==12;
	replace statecode=11 if st_code55==13;
	replace statecode=12 if st_code55==14;
	replace statecode=13 if st_code55==15;
	replace statecode=14 if st_code55==16;
	replace statecode=15 if st_code55==18;
	replace statecode=16 if st_code55==19;
	replace statecode=17 if st_code55==20;
	replace statecode=18 if st_code55==21;
	replace statecode=19 if st_code55==22;
	replace statecode=20 if st_code55==23;
	replace statecode=21 if st_code55==24;
	replace statecode=22 if st_code55==25;
	replace statecode=23 if st_code55==26;
	replace statecode=24 if st_code55==27;
	replace statecode=25 if st_code55==3;
	replace statecode=26 if st_code55==28;
	replace statecode=27 if st_code55==29;
	replace statecode=28 if st_code55==31;
	replace statecode=29 if st_code55==6;
	replace statecode=30 if st_code55==32;
	replace statecode=31 if st_code55==17;
	replace statecode=32 if st_code55==33;
	replace statecode=29 if st_code55==30;

	
	
	
	cap label drop states;
label define states

2	"Andhra Pradesh   "
3	"Assam            "
4	"Bihar            "
5	"Gujarat          "
6	"Haryana          "
7	"Himachal Pradesh "
8	"Jammu &Kashmir   "
9	"Karnataka        "
10	"Kerala           "
11	"Madhya Pradesh   "
12	"Maharashtra      "
13	"Manipur          "
14	"Meghalaya        "
15	"Nagaland         "
16	"Orissa           "
17	"Punjab		  "
18	"Rajasthan	  "	
19	"Sikkim           "
20	"Tamil Nadu	  "
21	"Tripura	  "
22	"Uttar Pradesh	  "
23	"West Bengal	  "
24	"Andaman & Nicobar Islands "
25	"Arunachal Pradesh"
26	"Chandigarh	  "
27	"Dadra &Nagar Haveli"
28	"Delhi            "
29	"Goa, Daman & Diu "
30	"Lakshadweep      "
31	"Mizoram          "
32	"Pondicherry      ";

cap label drop regions;
label define regions 

21	"Andhra Pradesh Costal"
22	"Andhra Pradesh Inland Northern"
23	"Andhra Pradesh South -Western"
24	"Andhra Pradesh Inland Southern"
31	"Arunachal Pradesh"
41	"Assam Plains Western"
42	"Assam Plains Western"
43	"Assam Hills"
51	"Bihar Southern"
52	"Bihar Northern"
53	"Bihar Central"
71	"Gujarat Plains and South-Eastern"
74	"Gujarat Dry areas"
75	"Gujarat Saurashtra"
81	"Haryana Eastern"
82	"Haryana Western"
91	"Himachal Pradesh"
101	"Jammu &Kashmir Mountainous"
102	"Jammu &Kashmir Outer Hills"
103	"Jammu &Kashmir Jhelam Valley"
111	"Karnataka Coastal & Ghats"
112	"Karnataka Inland Eastern"
113	"Karnataka Inland Southern"
114	"Karnataka Inland Nothern"
121	"Kerala Northern"
122	"Kerala Southern"
131	"Madhya Pradesh Chattisgarh"
132	"Madhya Pradesh Vindhya"
133	"Madhya Pradesh Central"
134	"Madhya Pradesh Malwa"
135	"Madhya Pradesh South"
136	"Madhya Pradesh South Western"
137	"Madhya Pradesh Northern"
141	"Maharashtra Coastal"
142	"Maharashtra Inland Western"
143	"Maharashtra Inland Northern"
144	"Maharashtra Inland Central"
145	"Maharashtra Inland Eastern"
146	"Maharashtra Eastern"
151	"Manipur Plains"
152	"Manipur Hills"
161	"Meghalaya"
171	"Mizoram"
181	"Nagaland"
191	"Orissa Coastal"
192	"Orissa Southern"
193	"Orissa Northern"
201	"Punjab Northern"
202	"Punjab Southern"
211	"Rajasthan Western"
212	"Rajasthan North-Eastern"
213	"Rajasthan Southern"
214	"Rajasthan South-Eastern"
221	"Sikkim"
231	"Tamil Nadu Coastal Northern"
232	"Tamil Nadu Coastal"
233	"Tamil Nadu Southern"
234	"Tamil Nadu Inland"
241	"Tripura"
251	"Uttar Pradesh Himalayan"
252	"Uttar Pradesh Western"
253	"Uttar Pradesh Central"
254	"Uttar Pradesh Eastern"
255	"Uttar Pradesh Southern"
261	"West Bengal Himalayan"
262	"West Bengal Eastern Plains"
263	"West Bengal Central Plains"
264	"West Bengal Western Plains"
271	"Andaman & Nicobar Islands"
281	"Chandigarh"
291	"Dadra &Nagar Haveli"
301	"Goa & Daman & Diu"
311	"Delhi"
321	"Lakshadweep"
331	"Pondicherry";

	
	# delimit cr
	
	drop st_code55 st_code61 reg55 reg61 statereg61 statereg55
	
	save mult61, replace
	
	
	
	
	forvalues k=1(1)1{
	
	use mult66`k', clear
	
	# delimit ;
	gen statereg66=region;
	gen st_code66=floor(statereg66/10);
	gen st_code55=.;
	replace st_code55=2 if st_code66==28;
	replace st_code55=3 if st_code66==12;
	replace st_code55=4 if st_code66==18;
	replace st_code55=5 if st_code66==10;
	replace st_code55=6 if st_code66==30;
	replace st_code55=7 if st_code66==24;
	replace st_code55=8 if st_code66==6;
	replace st_code55=9 if st_code66==2;
	replace st_code55=10 if st_code66==1;
	replace st_code55=11 if st_code66==29;
	replace st_code55=12 if st_code66==32;
	replace st_code55=13 if st_code66==23;
	replace st_code55=14 if st_code66==27;
	replace st_code55=15 if st_code66==14;
	replace st_code55=16 if st_code66==17;
	replace st_code55=17 if st_code66==15;
	replace st_code55=18 if st_code66==13;
	replace st_code55=19 if st_code66==21;
	replace st_code55=20 if st_code66==3;
	replace st_code55=21 if st_code66==8;
	replace st_code55=22 if st_code66==11;
	replace st_code55=23 if st_code66==33;
	replace st_code55=24 if st_code66==16;
	replace st_code55=25 if st_code66==9;
	replace st_code55=26 if st_code66==19;
	replace st_code55=27 if st_code66==35;
	replace st_code55=28 if st_code66==4;
	replace st_code55=29 if st_code66==26;
	replace st_code55=30 if st_code66==25;
	replace st_code55=31 if st_code66==7;
	replace st_code55=32 if st_code66==31;
	replace st_code55=33 if st_code66==34;
	*Chattisgarh used to be part of Madhya Pradesh;
	replace st_code55=13 if st_code66==22;
	*Uttaranchal used to be part of Uttar Pradesh;
	replace st_code55=25 if st_code66==5;
	*Jharkand used to be part of Bihar;
	replace st_code55=5 if st_code66==20;
	
	
	
	
	
	
	
	gen dist_code66=district;
	*Now convert regions between 66th and 61st rounds;
	gen reg66=statereg66-(10*st_code66);
	gen reg61=reg66;
	*They changed the regions in Andhra Pradesh;
	replace reg61=1 if reg66==2 & st_code66==28;
	replace reg61=2 if reg66==3 & st_code66==28;
	replace reg61=2 if reg66==4 & st_code66==28;
	replace reg61=3 if reg66==5 & st_code66==28 & (dist_code66==21 | dist_code==22);
	replace reg61=4 if reg66==5 & st_code66==28 & (dist_code66==20 | dist_code==23);
	
	*They changed the regions in Assam;
	replace reg61=1 if reg66==3 & st_code66==18 & (dist_code66>=21 & dist_code66<=23);
	replace reg61=2 if reg66==4 & st_code66==18;
	
	*They  changed the regions in Bihar - shuffled districts;
	replace reg61=2 if reg66==1 & st_code66==10 & (dist_code66>=20 & dist_code66<=21);
	
	*Chhatisgarh;
	replace reg61=1 if reg66==2 & st_code66==22;
	replace reg61=2 if reg66==3 & st_code66==22;
	
	
	*Gujarat;
	replace reg61=4 if reg66==5 & st_code66==24 & dist_code66==8;
	replace reg61=4 if reg66==3 & st_code66==24;
	replace reg61=1 if reg66==2 & st_code66==24;
	
	
	*Himachal Pradesh-- 
	*merge trans himalayan himachal pradesh into single region;
	replace reg61=1 if reg66==2 & st_code66==2;
	
	*Jammu and Kashmir;
	replace reg61=3 if reg66==4 & st_code66==1;
	
	*Jharkand;
	replace reg61=1 if reg66==2 & st_code66==20;
	
	*Orissa;
	replace reg61=1 if reg66==2 & st_code66==21 & (dist_code66>=19 & dist_code66<=20);
	replace reg61=3 if reg66==2 & st_code66==21 & (dist_code66>=23 & dist_code66<=24);

	*Rajasthan;
	replace reg61=1 if st_code66==8 & dist_code66==4;
	replace reg61=1 if st_code66==8 & dist_code66==14;
	replace reg61=1 if st_code66==8 & dist_code66<=2 & dist_code66>=1;
	
	replace reg61=2 if st_code66==8 & dist_code66==5;
	replace reg61=2 if st_code66==8 & dist_code66==13;
	
	*Uttar Pradesh;
	replace reg61=1 if st_code66==9 & reg66==5;
	
	*West Bengal;
	replace reg61=3 if reg66==4 & st_code66==19;
	replace reg61=4 if reg66==5 & st_code66==19;
	
	
	*Now convert regions between 61st and 55th rounds;
	gen reg55=reg61;
	*Jharkand split off but was region 1 of Bihar in 55th round;
	replace reg55=2 if reg61==1 & st_code66==10;
	replace reg55=3 if reg61==2 & st_code66==10;
	replace reg55=1 if reg61==1 & st_code66==20;
	
	*Chattisgarh split off but was region 1 of Madhya Pradesh in 55th round;
	replace reg55=1 if reg61==1 & st_code66==4;
	replace reg55=2 if reg61==1 & st_code66==23;
	replace reg55=3 if reg61==2 & st_code66==23;
	replace reg55=4 if reg61==3 & st_code66==23;
	replace reg55=5 if reg61==4 & st_code66==23;
	replace reg55=6 if reg61==5 & st_code66==23;
	replace reg55=7 if reg61==6 & st_code66==23;
	
	*Uttaranchal split off but was region of Uttar Pradesh in 55th round;
	replace reg55=1 if reg61==1 & st_code66==5;
	replace reg55=2 if reg61==1 & st_code66==9;
	replace reg55=3 if reg61==2 & st_code66==9;
	replace reg55=4 if reg61==3 & st_code66==9;
	replace reg55=5 if reg61==4 & st_code66==9;
	
	
	gen statereg55=(st_code55*10)+reg55;

	
	***Need to do district adjustments for certain regions?;
	
	
	gen regioncode=statereg55;
	
	*Goa and Daman and Diu are combined in a region in earlier rounds?;
	replace regioncode=301 if regioncode==61;

	
	*now convert to common state codes of earlier rounds;
	gen statecode=.;
	replace statecode=2 if st_code55==2;
	replace statecode=3 if st_code55==4;
	replace statecode=4 if st_code55==5;
	replace statecode=5 if st_code55==7;
	replace statecode=6 if st_code55==8;
	replace statecode=7 if st_code55==9;
	replace statecode=8 if st_code55==10;
	replace statecode=9 if st_code55==11;
	replace statecode=10 if st_code55==12;
	replace statecode=11 if st_code55==13;
	replace statecode=12 if st_code55==14;
	replace statecode=13 if st_code55==15;
	replace statecode=14 if st_code55==16;
	replace statecode=15 if st_code55==18;
	replace statecode=16 if st_code55==19;
	replace statecode=17 if st_code55==20;
	replace statecode=18 if st_code55==21;
	replace statecode=19 if st_code55==22;
	replace statecode=20 if st_code55==23;
	replace statecode=21 if st_code55==24;
	replace statecode=22 if st_code55==25;
	replace statecode=23 if st_code55==26;
	replace statecode=24 if st_code55==27;
	replace statecode=25 if st_code55==3;
	replace statecode=26 if st_code55==28;
	replace statecode=27 if st_code55==29;
	replace statecode=28 if st_code55==31;
	replace statecode=29 if st_code55==6;
	replace statecode=30 if st_code55==32;
	replace statecode=31 if st_code55==17;
	replace statecode=32 if st_code55==33;
	replace statecode=29 if st_code55==30;

	
	
	
	cap label drop states;
label define states

2	"Andhra Pradesh   "
3	"Assam            "
4	"Bihar            "
5	"Gujarat          "
6	"Haryana          "
7	"Himachal Pradesh "
8	"Jammu &Kashmir   "
9	"Karnataka        "
10	"Kerala           "
11	"Madhya Pradesh   "
12	"Maharashtra      "
13	"Manipur          "
14	"Meghalaya        "
15	"Nagaland         "
16	"Orissa           "
17	"Punjab		  "
18	"Rajasthan	  "	
19	"Sikkim           "
20	"Tamil Nadu	  "
21	"Tripura	  "
22	"Uttar Pradesh	  "
23	"West Bengal	  "
24	"Andaman & Nicobar Islands "
25	"Arunachal Pradesh"
26	"Chandigarh	  "
27	"Dadra &Nagar Haveli"
28	"Delhi            "
29	"Goa, Daman & Diu "
30	"Lakshadweep      "
31	"Mizoram          "
32	"Pondicherry      ";

cap label drop regions;
label define regions 

21	"Andhra Pradesh Costal"
22	"Andhra Pradesh Inland Northern"
23	"Andhra Pradesh South -Western"
24	"Andhra Pradesh Inland Southern"
31	"Arunachal Pradesh"
41	"Assam Plains Western"
42	"Assam Plains Western"
43	"Assam Hills"
51	"Bihar Southern"
52	"Bihar Northern"
53	"Bihar Central"
71	"Gujarat Plains and South-Eastern"
74	"Gujarat Dry areas"
75	"Gujarat Saurashtra"
81	"Haryana Eastern"
82	"Haryana Western"
91	"Himachal Pradesh"
101	"Jammu &Kashmir Mountainous"
102	"Jammu &Kashmir Outer Hills"
103	"Jammu &Kashmir Jhelam Valley"
111	"Karnataka Coastal & Ghats"
112	"Karnataka Inland Eastern"
113	"Karnataka Inland Southern"
114	"Karnataka Inland Nothern"
121	"Kerala Northern"
122	"Kerala Southern"
131	"Madhya Pradesh Chattisgarh"
132	"Madhya Pradesh Vindhya"
133	"Madhya Pradesh Central"
134	"Madhya Pradesh Malwa"
135	"Madhya Pradesh South"
136	"Madhya Pradesh South Western"
137	"Madhya Pradesh Northern"
141	"Maharashtra Coastal"
142	"Maharashtra Inland Western"
143	"Maharashtra Inland Northern"
144	"Maharashtra Inland Central"
145	"Maharashtra Inland Eastern"
146	"Maharashtra Eastern"
151	"Manipur Plains"
152	"Manipur Hills"
161	"Meghalaya"
171	"Mizoram"
181	"Nagaland"
191	"Orissa Coastal"
192	"Orissa Southern"
193	"Orissa Northern"
201	"Punjab Northern"
202	"Punjab Southern"
211	"Rajasthan Western"
212	"Rajasthan North-Eastern"
213	"Rajasthan Southern"
214	"Rajasthan South-Eastern"
221	"Sikkim"
231	"Tamil Nadu Coastal Northern"
232	"Tamil Nadu Coastal"
233	"Tamil Nadu Southern"
234	"Tamil Nadu Inland"
241	"Tripura"
251	"Uttar Pradesh Himalayan"
252	"Uttar Pradesh Western"
253	"Uttar Pradesh Central"
254	"Uttar Pradesh Eastern"
255	"Uttar Pradesh Southern"
261	"West Bengal Himalayan"
262	"West Bengal Eastern Plains"
263	"West Bengal Central Plains"
264	"West Bengal Western Plains"
271	"Andaman & Nicobar Islands"
281	"Chandigarh"
291	"Dadra &Nagar Haveli"
301	"Goa & Daman & Diu"
311	"Delhi"
321	"Lakshadweep"
331	"Pondicherry";

	# delimit cr

	drop statereg55 dist_code66 reg55 reg61 reg66 st_code66
	save mult66`k', replace
	}
	
	
	
	
	
	
	
	foreach k in 38 43 50 55 61 661{
	use mult`k', clear
	egen numregion=group(regioncode)
	egen numstate=group(statecode)
	summ numregion numstate
}



use mult661, clear
save mult66, replace





**************Step 1: prepare for re-shaping





*********************Round 38******************
**linking variables:  sector region subround subsample fsu hhno
cd "`rootdir'Round38\"


use round38_cedit, clear
***lots of missing value fields
drop if value==0 & (homevalue==0 | homevalue==.)
**116,832 households

***Keeping the biggest duplicates***********
bysort sector region subround subsample fsu hhno item type: egen maxvalue=max(value)
keep if value==maxvalue
duplicates drop sector region subround subsample fsu hhno item type, force

***type variable is for clothing -- material/source
***if there are multiple types we should combined them into one item
foreach j in value quant homevalue homequant{
bysort sector region subround subsample fsu hhno item: egen `j'temp=total(`j')
replace `j'=`j'temp
drop `j'temp
}
duplicates drop sector region subround subsample fsu hhno item, force
drop type 


ren value x
ren quant q
ren homevalue hx
ren homequant hq
egen id=group(sector region subround subsample fsu hhno)
egen z=group(sector subround)
save "`rootdir'round38_cedit", replace


*********************Round 43******************
**linking variables:  sector region subsample subround fsu substratum hhno

cd "`rootdir'Round43"


use round43_cedit, clear
drop if value==0 & (homevalue==0 | homevalue==.)

*duplicates drop sector region subsample subround fsu substratum hhno, force


***Keeping the biggest duplicates***********
bysort sector fsu region subsample subround substratum hhno item type: egen maxvalue=max(value)
keep if value==maxvalue
duplicates drop sector fsu region subsample subround substratum hhno item type, force

***type variable is for clothing -- material/source
***if there are multiple types we should combined them into one item
foreach j in value quant homevalue homequant{
bysort sector fsu region subsample subround substratum hhno item: egen `j'temp=total(`j')
replace `j'=`j'temp
drop `j'temp
}
duplicates drop sector fsu region subsample subround substratum hhno item, force
drop type 


ren value x
ren quant q
ren homevalue hx
ren homequant hq
egen id=group(sector fsu region subsample subround substratum hhno)
egen z=group(sector subround)
save "`rootdir'round43_cedit", replace







*********************Round 55******************two types
****Linking variables: sector region subround subsample fsu segment substratum hhno
cd "`rootdir'Round55\"

use round55_cedit, clear
drop if value==0
*duplicates drop sector region subround subsample fsu segment ssstratum hhno, force
*120,350 households.


***Keeping the biggest duplicates***********
bysort sector region subround subsample fsu segment substratum hhno item: egen maxvalue=max(value)
keep if value==maxvalue
duplicates drop sector region subround subsample fsu segment substratum hhno item, force



***Assumption is that category 3 (combined) and 4 (free collection) count as 100% home produced.
gen q=quant
gen x=value

gen hx=x if (source==2 | source==3 | source==4)
replace hx=0 if hx==.
gen hq=q if (source==2 | source==3 | source==4)
replace hq=0 if hq==.

***type variable is for clothing -- material/source
***if there are multiple types we should combined them into one item
*foreach j in value quant homevalue homequant{
*bysort sector region subround subsample fsu segment substratum hhno item : egen `j'temp=total(`j')
*replace `j'=`j'temp
*drop `j'temp
*}

drop source value quant



egen id=group(sector region subround subsample fsu segment substratum hhno)
egen z=group(sector subround)
save "`rootdir'round55_cedit", replace






*********************Round 61******************
**Linking variables   fsu sector region stratum substratum subround subsample block ssstratum hhno
cd "`rootdir'Round61\"

clear
use round61_cedit
drop if value==0 & (homevalue==. | homevalue==0)

*duplicates drop sector region subround subsample segment ssstratum fsu hhno, force
*124,644 households

***Keeping the biggest duplicates***********
bysort sector region subround subsample segment substratum fsu hhno item: egen maxvalue=max(value)
keep if value==maxvalue
duplicates drop sector region subround subsample segment substratum fsu hhno item, force

duplicates drop sector region subround subsample segment substratum fsu hhno item, force


ren value x
ren quant q
ren homevalue hx
ren homequant hq
egen id=group(sector region subround subsample segment substratum fsu hhno)
egen z=group(sector subround)
save "`rootdir'round61_cedit", replace


*********************Round 66******************two types
******Linking variables: sector region subround subsample fsu segment ssstratum hhno

cd "`rootdir'Round66\"

use round661_cedit, clear
drop if value==0 & (homevalue==. | homevalue==0)

*duplicates drop sector region subround subsample segment ssstratum fsu hhno, force
*100,855 households

***Keeping the biggest duplicates***********
bysort sector region subround subsample segment substratum fsu hhno item: egen maxvalue=max(value)
keep if value==maxvalue
duplicates drop sector region subround subsample segment substratum fsu hhno item, force


ren value x
ren quant q
ren homevalue hx
ren homequant hq
egen id=group(sector region subround subsample segment substratum fsu hhno)
egen z=group(sector subround)
save "`rootdir'round661_cedit", replace



**************Step 2: re-shape [doing this in pieces -- sector subround z -- makes it possible with limited memory and lots of variables]


cd "`rootdir'
clear
set mem 10g

foreach round in 38 43 55 61 661{
clear
save round`round'_wide, emptyok replace

forvalues j=1(1)8{
use if z==`j' using round`round'_cedit, clear
drop maxvalue
reshape wide x q hx hq, i(id) j(item)
append using round`round'_wide
save round`round'_wide, replace
}
}




************Step 3: fix units

use round38_wide, clear


*drop quantity for durables
aorder

drop q651-q1050 hq651-hq1050 hx651-hx1050



***For round 38, need to multiply up by 100 if it's a number and not a quantity in kg
foreach i in q161 q162 q171 q241 q244 q245 q246 q248 q291 q292 q293 q294 q295 q296 q297 q299 q301 q303 q306 q307 q308 q314 q316 q317 q318{
replace `i'=`i'*100
}

*non-food
foreach i in q401 q402 q411 q412 q416 q504 q506 q514 q607 q608 q611 q616 q618 q621 q629{
replace `i'=`i'*100
}


*chicken, birds, eggs   - 1.5 conversion factor too big?, use 1:1 for now, seems to match data
*replace q161=q161*1.5
*replace q162=q162*1.5

replace q171=q171*0.06
*172- egg products? appears to be in kg


*bananas, pineapple, coconut, guava, orange
replace q241=q241*0.2
replace q244=q244*2
replace q245=q245*0.75
replace q246=q246*0.05
replace q248=q248*0.25

*lemon is in kg

*spices - convert from grams to kg
forvalues j=291(1)297{
replace q`j'=q`j'/1000
}
replace q299=q299/1000

*315 - cake, units? appears to be in kg?

*convert pickles, sauce, jellies, other proc. foods?
forvalues j=316(1)319{
replace q`j'=q`j'/1000
}


*convert intoxicants that are in grams (need to multiply by factor of 100, don't know why)
foreach j in q403 q404 q405 q409 q414 q417 q419 q421 q424 q429{
replace `j'=`j'*100
}


*fuel light
replace q503=q503*100

***gas/LPG - convert from standard unit to LPG, x10 at least, but not really sure
replace q511=q511*10

*what is this?
replace q302=q302/10
replace q304=q304/10

*********H version

**new stuff
foreach p in h{

foreach i in `p'q161 `p'q162 `p'q171 `p'q241 `p'q244 `p'q245 `p'q246 `p'q248 `p'q291 `p'q292 `p'q293 `p'q294 `p'q295 `p'q296 `p'q297 `p'q299 `p'q301 `p'q303 `p'q306 `p'q307 `p'q308 `p'q314 `p'q316 `p'q317 `p'q318{
replace `i'=`i'*100
}

*non-food
foreach i in `p'q401 `p'q402 `p'q411 `p'q412 `p'q416 `p'q504 `p'q506 `p'q514 `p'q607 `p'q608 `p'q611 `p'q616 `p'q618 `p'q621 `p'q629{
replace `i'=`i'*100
}


*chicken, birds, eggs   - 1.5 conversion factor too big?, use 1:1 for now, seems to match data
*replace q161=q161*1.5
*replace q162=q162*1.5

replace `p'q171=`p'q171*0.06
*172- egg products? appears to be in kg


*bananas, pineapple, coconut, guava, orange
replace `p'q241=`p'q241*0.2
replace `p'q244=`p'q244*2
replace `p'q245=`p'q245*0.75
replace `p'q246=`p'q246*0.05
replace `p'q248=`p'q248*0.25

*lemon is in kg

*spices - convert from grams to kg
forvalues j=291(1)297{
replace `p'q`j'=`p'q`j'/1000
}
replace `p'q299=`p'q299/1000

*315 - cake, units? appears to be in kg?

*convert pickles, sauce, jellies, other proc. foods?
forvalues j=316(1)319{
replace `p'q`j'=`p'q`j'/1000
}


*convert intoxicants that are in grams (need to multiply by factor of 100, don't know why)
foreach j in `p'q403 `p'q404 `p'q405 `p'q409 `p'q414 `p'q417 `p'q419 `p'q421 `p'q424 `p'q429{
replace `j'=`j'*100
}


}


*fuel light
replace hq503=hq503*100

***gas/LPG - convert from standard unit to LPG, x10 at least, but not really sure
replace hq511=hq511*10


replace hq302=hq302/10
replace hq304=hq304/10






***Dropping goods that shouldn't be in the survey (data entry errors)
drop x517 x518 x509 
drop q517 q518 q509

drop x521-x581
drop q521-q581

drop x643 x644 q643 q644 x619 q619 x624 q624 
drop x634 x635 x636 x638 q634 q635 q636 q638 x639 q639
drop x651-x699



drop hx517 hx518 hq517 hq518 hx509 hq509
drop hx521-hx581 hq521-hq581
drop hx643 hx644 hq643 hq644 hx619 hq619 hx624 hq624 
drop hx634 hx635 hx636 hx638 hq634 hq635 hq636 hq638 hx639 hq639




drop x909 x914-x918 x929 x932-x937 x944-x948 x969 x972-x978 x986-x988 x995-x998 x1006-x1008 x1012-x1018 x1035-x1038
drop x708 x717 x718 x736 x737 x738 x749 x753 x754 x755 x756 x757 x758 x768 x779 x783 x784 x785 x786 x788 x792 x793 x794 x795 x796 x797 x798 x799
drop x801 x802 x807 x808 x815
drop x821-x893
drop x1021 x1022 x1025 x1027 x1044 x1048



save round38_wide_edit, replace








use round43_wide, clear
aorder

drop q420-q688 hq420-hq688 hx420-hx688

*ice-cream
replace q136=q136/10

*chicken, birds, eggs  - don't adjust chicken for now, 1:1 seems to match data better
*replace q170=q170*1.5
*replace q171=q171*1.5

replace q172=q172*0.06

*bananas, pineapple, coconut, guava, orange
replace q230=q230*0.2
replace q233=q233*2
replace q234=q234*0.75
replace q235=q235*0.05
replace q237=q237*0.25

*spices, grams to kg
forvalues j=280(1)286{
replace q`j'=q`j'/1000
}
replace q288=q288/1000

*coffee and tea conversions, from grams to kg - do kg to cups later?
replace q291=q291/1000
replace q293=q293/1000

*pickles, sauce, jelly, other processed food
forvalues j=315(1)318{
replace q`j'=q`j'/1000
}


foreach p in h{

*ice-cream
replace `p'q136=`p'q136/10

*chicken, birds, eggs  - don't adjust chicken for now, 1:1 seems to match data better
*replace q170=q170*1.5
*replace q171=q171*1.5

replace `p'q172=`p'q172*0.06

*bananas, pineapple, coconut, guava, orange
replace `p'q230=`p'q230*0.2
replace `p'q233=`p'q233*2
replace `p'q234=`p'q234*0.75
replace `p'q235=`p'q235*0.05
replace `p'q237=`p'q237*0.25

*spices, grams to kg
forvalues j=280(1)286{
replace `p'q`j'=`p'q`j'/1000
}
replace `p'q288=`p'q288/1000

*coffee and tea conversions, from grams to kg - do kg to cups later?
replace `p'q291=`p'q291/1000
replace `p'q293=`p'q293/1000

*pickles, sauce, jelly, other processed food
forvalues j=315(1)318{
replace `p'q`j'=`p'q`j'/1000
}

}


***Dropping goods that shouldn't be there (data entry errors)
*none

save round43_wide_edit, replace





foreach round in 55{
use round`round'_wide, clear
aorder

drop q300-q552 hq300-hq552 hx300-hx552
*no home production for clothing starting in 55th round
drop hx260-hx294 hq260-hq294



*eggs
replace q80=q80*0.06

*banana, pineapple, coconut, orange, lemon
replace q130=q130*0.2
replace q133=q133*2
replace q134=q134*0.75
replace q137=q137*0.25

replace q121=q121*0.17

*spices
forvalues j=180(1)188{
replace q`j'=q`j'/1000
}

*coffe/tea
replace q191=q191/1000
replace q193=q193/1000

*pickles, sauces, jelly
forvalues j=205(1)207{
replace q`j'=q`j'/1000
}

*tobacco
*matches, candles

foreach p in h{

*eggs
replace `p'q80=`p'q80*0.06

*banana, pineapple, coconut, orange, lemon
replace `p'q130=`p'q130*0.2
replace `p'q133=`p'q133*2
replace `p'q134=`p'q134*0.75
replace `p'q137=`p'q137*0.25

replace `p'q121=`p'q121*0.17

*spices
forvalues j=180(1)188{
replace `p'q`j'=`p'q`j'/1000
}

*coffe/tea
replace `p'q191=`p'q191/1000
replace `p'q193=`p'q193/1000

*pickles, sauces, jelly
forvalues j=205(1)207{
replace `p'q`j'=`p'q`j'/1000
}

}

***Dropping missing items.  data entry errors


drop x19 q19 x22 q22 x28 q28 x32 q32 x36 q36
drop hx19 hq19 hx22 hq22 hx28 hq28 hx32 hq32 hx36 hq36

drop x55 x58 q55 q58 x99 q99 x109 q109 x119 q119 x128 q128 x139 q139 q148 x148 x170 q170 x173 q173 x217 q217 q237 x237 x254 q254 x277 q277 x278 q278 x288 q288 x297 q297
drop hx55 hx58 hq55 hq58 hx99 hq99 hx109 hq109 hx119 hq119 hx128 hq128 hx139 hq139 hq148 hx148 hx170 hq170 hx173 hq173 hx217 hq217 hq237 hx237 hx254 hq254 




drop x307 x328 x397 x442 x486 x501 x502 x503 x524 x544 x550 x552




save round`round'_wide_edit, replace
}



clear
use round61_wide
aorder

drop q300-q543 hx300-hx543 hq300-hq543
drop hx260-hx294 hq260-hq294


***Need to correct for fact that all quantities are inflated by factor of 10 (three decimal zeros)


*eggs
replace q80=q80*0.06

*banana, pineapple, coconut, orange, lemon
replace q130=q130*0.2
replace q133=q133*2
replace q134=q134*0.75
replace q137=q137*0.25
replace q121=q121*0.2

**converting grams into KG

*spices
forvalues j=180(1)186{
replace q`j'=q`j'/1000
}

replace q122=q122/1000
replace q123=q123/1000

*coffee/tea
replace q191=q191/1000
replace q193=q193/1000

*soft drink, fruit juice convert from literes to no. (assume 4 no. in a liter)
replace q195=q195*4
replace q196=q196*4

*pickles, sauces, jelly
forvalues j=205(1)207{
replace q`j'=q`j'/1000
}


*tobacco
*matches, candles



*make like round55?

gen q188=q186
gen x188=x186
drop q186 x186

gen q187=q185
gen x187=x185
drop x185 q185

gen q186=q184
gen x186=x184
drop x184 q184

gen q184=q183
gen x184=x183
drop x183 q183

gen q183=q122
gen x183=x122
drop q122 x122

gen q185=q123
gen x185=x123
drop q123 x123

gen q122=q124
gen x122=x124
drop q124 x124


replace q222=q222*1000
replace q224=q224*1000


foreach p in h{


*eggs
replace `p'q80=`p'q80*0.06

*banana, pineapple, coconut, orange, lemon
replace `p'q130=`p'q130*0.2
replace `p'q133=`p'q133*2
replace `p'q134=`p'q134*0.75
replace `p'q137=`p'q137*0.25
replace `p'q121=`p'q121*0.2

*spices
forvalues j=180(1)186{
replace `p'q`j'=`p'q`j'/1000
}

replace `p'q122=`p'q122/1000
replace `p'q123=`p'q123/1000

*coffee/tea
replace `p'q191=`p'q191/1000
replace `p'q193=`p'q193/1000

*soft drink, fruit juice convert from literes to no. (assume 4 no. in a liter)
replace `p'q195=`p'q195*4
replace `p'q196=`p'q196*4

*pickles, sauces, jelly
forvalues j=205(1)207{
replace `p'q`j'=`p'q`j'/1000
}


*tobacco
*matches, candles



*make like round55

gen `p'q188=`p'q186
gen `p'x188=`p'x186
drop `p'q186 `p'x186

gen `p'q187=`p'q185
gen `p'x187=`p'x185
drop `p'x185 `p'q185

gen `p'q186=`p'q184
gen `p'x186=`p'x184
drop `p'x184 `p'q184

gen `p'q184=`p'q183
gen `p'x184=`p'x183
drop `p'x183 `p'q183

gen `p'q183=`p'q122
gen `p'x183=`p'x122
drop `p'q122 `p'x122

gen `p'q185=`p'q123
gen `p'x185=`p'x123
drop `p'q123 `p'x123

gen `p'q122=`p'q124
gen `p'x122=`p'x124
drop `p'q124 `p'x124



}


replace hq222=hq222*1000
replace hq224=hq224*1000



save round61_wide_edit, replace




***round 66

*Some cleaning and harmonizing of units

foreach round in 661{
use round`round'_wide, clear
aorder

drop q300-q543
drop hx260-hx543 hq260-hq543


***Need to correct for fact that all quantities are inflated by factor of 10 (three decimal zeros)
foreach j in q100 q141 q150 q153 q154 q155 q158 q180 q181 q182 q183 q184 q185 q186 q187 q188 q190 q191 q192 q193 q202 q203 q205 q206 q207 q211 q212 q220 q221 q222 q223 q224 q225 q226 q230 q242 q246 q251 q260 q261 q264 q265 q266 q267 q268 q270 q271 q280 q281 q282 q284 q285 q286{
replace `j'=`j'*1000
}


*eggs
replace q100=q100*0.06

*banana, pineapple, coconut, orange, lemon
replace q150=q150*0.2
replace q153=q153*2
replace q154=q154*0.75
replace q158=q158*0.25
replace q141=q141*0.2

***Need to correct for all the 1000 decimal factors for spices and such
*spices
forvalues j=180(1)188{
replace q`j'=q`j'/1000
}

*coffee/tea
replace q191=q191/1000
replace q193=q193/1000

*soft drink, fruit juice convert from literes to no. (assume 4 no. in a liter)
replace q195=q195*4
replace q196=q196*4

*pickles, sauces, jelly
forvalues j=205(1)207{
replace q`j'=q`j'/1000
}

*Dont care about intoxicants (items 223,224,225...)?
*tobacco
*matches, candles


*make like round61

gen q53=q52
gen x53=x52
replace q52=q51
replace x52=x51
replace q51=q50
replace x51=x50
replace q50=q48
replace x50=x48
replace q48=q47
replace x48=x47
drop q47 x47

***note: setting soyabean to zero but leaving it in there!
gen q47=.
gen x47=0

gen q160a = q70
gen x160a = x70
gen q161a = q71
gen x161a = x71
gen q162a = q72
gen x162a = x72
gen q163a = q73
gen x163a = x73
gen q164a = q74
gen x164a = x74
gen q179a = q89
gen x179a = x89

drop q70 x70 q71 x71 q72 x72 q73 x73 q74 x74 q89 x89

rename x142 x144
rename q142 q144



foreach i of numlist 90 91 92 93 94 100 101 102 103 104 105 106 110 111 112 113 114 115 116 117 118 120 121 122 123 124 125 126 127 128 130/138 140/141 144 150/154 {
	local d = `i'-20
	rename q`i' q`d'
	rename x`i' x`d'
	}

gen q197a=q155
gen x197a=x155

drop q155 x155

foreach i of numlist 156/158 161/168 {
	local d = `i'-21
	rename q`i' q`d'
	rename x`i' x`d'
	}
	
rename q160 q138
rename x160 x138
	
foreach i of numlist 170/177 {
	local d = `i'-20
	rename q`i' q`d'
	rename x`i' x`d'
	}
	
rename q160a q160
rename x160a x160
rename q161a q161
rename x161a x161
rename q162a q162
rename x162a x162
rename q163a q163
rename x163a x163
rename q164a q164
rename x164a x164
rename q179a q179
rename x179a x179

rename q180 q122
rename x180 x122
rename q181 q123
rename x181 x123

foreach i of numlist 182/188 {
	local d = `i'-2
	rename q`i' q`d'
	rename x`i' x`d'
	}

replace q195=q195+q194 
replace x195=x195+x194 
replace q194 = .
replace x194 = .

gen q198a = q197
gen x198a = x197

drop q197 x197

rename q197a q197
rename x197a x197

gen q200a = q198
gen x200a = x198

drop q198 x198

rename q198a q198
rename x198a x198

gen q204a = q200
gen x204a = x200

drop q200 x200

rename q200a q200
rename x200a x200

gen q202a = q201
gen x202a = x201

drop q201 x201


gen xfreemeal=x202
gen qfreemeal=q202
drop x202 q202

gen q201=q204
gen x201=x204

drop q204 x204

rename q204a q204
rename x204a x204

rename q202a q202
rename x202a x202

rename q212 q215
rename x212 x215



*make like round55

gen q188=q186
gen x188=x186
drop q186 x186

gen q187=q185
gen x187=x185
drop x185 q185

gen q186=q184
gen x186=x184
drop x184 q184

gen q184=q183
gen x184=x183
drop x183 q183

gen q183=q122
gen x183=x122
drop q122 x122

gen q185=q123
gen x185=x123
drop q123 x123

gen q122=q124
gen x122=x124
drop q124 x124


replace q222=q222*1000
replace q224=q224*1000




****Clothing, footwear and other stuff





replace q253=q253+q254+q255
replace x253=x253+x254+x255

drop q254 q255 x254 x255

*Second hand footwear was not a category in NSS 61. I have included it in "other footware" so that it will be captured in aggregations.
replace q294=q294+q295
replace x294=x294+x295

drop q295 x295

replace x300=x300+x301

drop x301

foreach i of numlist 302/306 {
	local d = `i'-1
	rename x`i' x`d'
	}
	
gen x306 = x307+x308

drop x307 x308

foreach i of numlist 354/357 {
	local d = `i'+1
	rename x`i' x`d'a
	}
	
foreach i of numlist 355/358 {
	rename x`i'a x`i'
	}	

gen x354=.

replace x396 = x396+x381

drop x381

foreach i of numlist 382/385 {
	local d = `i'-1
	rename x`i' x`d'
	}
	
gen x385=x392

drop x392

gen x391a = x386

drop x386

replace x388=x388+x387

drop x387

gen x387=x390

drop x390

gen x392=x391

drop x391

rename x391a x391

gen x386=x393

drop x393

gen x390=x394

drop x394

gen x393=x395

drop x395

gen x394=x396

drop x396

replace x423=x423+x421

drop x421

gen x421=x422

drop x422

gen x422=x423

drop x423

foreach i of numlist 460/464 {
	local d = `i'+1
	rename x`i' x`d'a
	}	

gen x460=.

foreach i of numlist 465/466 {
	local d = `i'+2
	rename x`i' x`d'a
	}
	
foreach i of numlist 461/465 {
	rename x`i'a x`i'
	}

foreach i of numlist 467/468 {
	rename x`i'a x`i'
	}

gen x466=.

gen x480a=x470
gen x481a=x471
gen x482a=x472
gen x483a=x473

drop x470 x471 x472 x473

rename x540 x470
rename x541 x471
rename x542 x472
rename x543 x473

replace x491=x491+x488

drop x488

foreach i of numlist 481/487 {
	local d = `i'+1
	rename x`i' x`d'aa
	}

foreach i of numlist 482/488 {
	rename x`i'aa x`i'
	}

gen x481=.

foreach i of numlist 480/488 490 491 {
	local d = `i'+10
	rename x`i' x`d'a
	}

foreach i of numlist 500/504 510 511 {
	local d = `i'+10
	rename x`i' x`d'a
	}

replace x524=x524+x525

drop x525

foreach i of numlist 520/524 530/533 {
	local d = `i'+10
	rename x`i' x`d'a
	}

foreach i of numlist 480/483 490/498 500 501 510/514 520 521 530/534 540/543 {
	rename x`i'a x`i'
	}




****repeat for hom stuff


foreach p in h{


*correcting for 1000
foreach j in `p'q100 `p'q141 `p'q150 `p'q153 `p'q154 `p'q155 `p'q158 `p'q180 `p'q181 `p'q182 `p'q183 `p'q184 `p'q185 `p'q186 `p'q187 `p'q188 `p'q190 `p'q191 `p'q192 `p'q193 `p'q202 `p'q203 `p'q205 `p'q206 `p'q207  `p'q211 `p'q212 `p'q220 `p'q221 `p'q222 `p'q223 `p'q224 `p'q225 `p'q226 `p'q230 `p'q242 `p'q246 `p'q251{
replace `j'=`j'*1000
}

*eggs
replace `p'q100=`p'q100*0.06
*banana, pineapple, coconut, orange, lemon
replace `p'q150=`p'q150*0.2
replace `p'q153=`p'q153*2
replace `p'q154=`p'q154*0.75
replace `p'q158=`p'q158*0.25
replace `p'q141=`p'q141*0.2

*spices
forvalues j=180(1)188{
replace `p'q`j'=`p'q`j'/1000
}

*coffee/tea
replace `p'q191=`p'q191/1000
replace `p'q193=`p'q193/1000

*soft drink, fruit juice convert from literes to no. (assume 4 no. in a liter)
replace `p'q195=`p'q195*4
replace `p'q196=`p'q196*4

*pickles, sauces, jelly
forvalues j=205(1)207{
replace `p'q`j'=`p'q`j'/1000
}

*Dont care about intoxicants (items 223,224,225...)?
*tobacco
*matches, candles


*make like round61...

gen `p'q53=`p'q52
gen `p'x53=`p'x52
replace `p'q52=`p'q51
replace `p'x52=`p'x51
replace `p'q51=`p'q50
replace `p'x51=`p'x50
replace `p'q50=`p'q48
replace `p'x50=`p'x48
replace `p'q48=`p'q47
replace `p'x48=`p'x47
drop `p'q47 `p'x47

gen `p'q47=.
gen `p'x47=0

gen `p'q160a = `p'q70
gen `p'x160a = `p'x70
gen `p'q161a = `p'q71
gen `p'x161a = `p'x71
gen `p'q162a = `p'q72
gen `p'x162a = `p'x72
gen `p'q163a = `p'q73
gen `p'x163a = `p'x73
gen `p'q164a = `p'q74
gen `p'x164a = `p'x74
gen `p'q179a = `p'q89
gen `p'x179a = `p'x89

drop `p'q70 `p'x70 `p'q71 `p'x71 `p'q72 `p'x72 `p'q73 `p'x73 `p'q74 `p'x74 `p'q89 `p'x89

rename `p'x142 `p'x144
rename `p'q142 `p'q144

foreach i of numlist 90 91 92 93 94 100 101 102 103 104 105 106 110 111 112 113 114 115 116 117 118 120 121 122 123 124 125 126 127 128 130/138 140/141 144 150/154 {
	local d = `i'-20
	rename `p'q`i' `p'q`d'
	rename `p'x`i' `p'x`d'
	}

gen `p'q197a=`p'q155
gen `p'x197a=`p'x155

drop `p'q155 `p'x155

foreach i of numlist 156/158 161/168 {
	local d = `i'-21
	rename `p'q`i' `p'q`d'
	rename `p'x`i' `p'x`d'
	}
	
rename `p'q160 `p'q138
rename `p'x160 `p'x138
	
foreach i of numlist 170/177 {
	local d = `i'-20
	rename `p'q`i' `p'q`d'
	rename `p'x`i' `p'x`d'
	}
	
rename `p'q160a `p'q160
rename `p'x160a `p'x160
rename `p'q161a `p'q161
rename `p'x161a `p'x161
rename `p'q162a `p'q162
rename `p'x162a `p'x162
rename `p'q163a `p'q163
rename `p'x163a `p'x163
rename `p'q164a `p'q164
rename `p'x164a `p'x164
rename `p'q179a `p'q179
rename `p'x179a `p'x179

rename `p'q180 `p'q122
rename `p'x180 `p'x122
rename `p'q181 `p'q123
rename `p'x181 `p'x123

foreach i of numlist 182/188 {
	local d = `i'-2
	rename `p'q`i' `p'q`d'
	rename `p'x`i' `p'x`d'
	}

replace `p'q195=`p'q195+`p'q194 
replace `p'x195=`p'x195+`p'x194 
replace `p'q194 = .
replace `p'x194 = .

gen `p'q198a = `p'q197
gen `p'x198a = `p'x197

drop `p'q197 `p'x197

rename `p'q197a `p'q197
rename `p'x197a `p'x197

gen `p'q200a = `p'q198
gen `p'x200a = `p'x198

drop `p'q198 `p'x198

rename `p'q198a `p'q198
rename `p'x198a `p'x198

gen `p'q204a = `p'q200
gen `p'x204a = `p'x200

drop `p'q200 `p'x200

rename `p'q200a `p'q200
rename `p'x200a `p'x200

gen `p'q202a = `p'q201
gen `p'x202a = `p'x201

drop `p'q201 `p'x201

replace `p'q203=`p'q203
*+`p'q202
replace `p'x203=`p'x203
*+`p'x202

drop `p'q202 `p'x202

gen `p'q201=`p'q204
gen `p'x201=`p'x204

drop `p'q204 `p'x204

rename `p'q204a `p'q204
rename `p'x204a `p'x204

rename `p'q202a `p'q202
rename `p'x202a `p'x202

rename `p'q212 `p'q215
rename `p'x212 `p'x215

*The code for information up to block 9 is the same as in NSS 61 (differences after block 9 - items 400+)

*make like round55

gen `p'q188=`p'q186
gen `p'x188=`p'x186
drop `p'q186 `p'x186

gen `p'q187=`p'q185
gen `p'x187=`p'x185
drop `p'x185 `p'q185

gen `p'q186=`p'q184
gen `p'x186=`p'x184
drop `p'x184 `p'q184

gen `p'q184=`p'q183
gen `p'x184=`p'x183
drop `p'x183 `p'q183

gen `p'q183=`p'q122
gen `p'x183=`p'x122
drop `p'q122 `p'x122

gen `p'q185=`p'q123
gen `p'x185=`p'x123
drop `p'q123 `p'x123

gen `p'q122=`p'q124
gen `p'x122=`p'x124
drop `p'q124 `p'x124

}



replace hq222=hq222*1000
replace hq224=hq224*1000



save round`round'_wide_edit, replace
}










**********************************************
***********Step 4: combining goods and relabelling to get a common set of variables*******
**********************************************


use round38_wide_edit, clear

gen round=38
aorder

foreach i of varlist q1-q649 hq1-hq649{
replace `i'=0 if `i'==.
}

foreach i of varlist x1-x1050 hx1-hx649{
replace `i'=0 if `i'==.
}


gen xservants=x763



replace x281=x281+x282
replace q281=q281+q282
drop x282 q282

replace hx281=hx281+hx282
replace hq281=hq281+hq282
drop hx282 hq282


replace x519=x519+x508+x513+x515
replace q519=0
drop x508 x513 x515


replace hx519=hx519+hx508+hx513+hx515
replace hq519=0
drop hx508 hx513 hx515


replace x409=x403+x404+x405+x409
replace q409=q403+q404+q405+q409
drop x403 x404 x405 q403 q404 q405

replace hx409=hx403+hx404+hx405+hx409
replace hq409=hq403+hq404+hq405+hq409
drop hx403 hx404 hx405 hq403 hq404 hq405

gen x768=x631+x632+x633
gen q768=q631+q632+q633
replace q768=0
drop x631 x632 x633 q631 q632 q633

replace x712=x712+x713+x714
drop x713 x714

replace x721=x721+x722
drop x722

replace x1034=x1034+x724
drop x724

replace x729=x729+x728+x726
drop x726 x728

replace x748=x748+x751
drop x751

replace x789=x789+x781+x782+x773
drop x781 x782 x773

replace x902=x902+x903
drop x903

replace x907=x907+x904+x905
drop x904 x905

replace x919=x919+x906
drop x906

replace x938=x938+x921+x922
drop x921 x922

replace x925=x924+x925
drop x924

replace x952=x952+x953+x954+x955+x956
drop x953 x954 x955 x956

replace x959=x959+x957+x958
drop x957 x958

replace x989=x989+x985
drop x985

replace x991=x991+x992
drop x992

replace x999=x999+x993+x994
drop x993 x994

gen x736=x1001
drop x1001

gen x737=x1003
drop x1003 

replace x1009=x1009+x1004+x1005
drop x1004 x1005



***Drop unnecessary quantities
renpfix q qx
renpfix hq hqx



aorder
gen ngrains=0
gen nhgrains=0
gen xgrains=0
gen hxgrains=0


local counter=1
foreach i of varlist x1-x99{
replace ngrains=ngrains+1 if `i'>0
replace nhgrains=nhgrains+1 if h`i'>0
replace xgrains=xgrains+`i'
replace hxgrains=hxgrains+h`i'


gen xgrains_`counter'=`i'
gen qgrains_`counter'=q`i'
gen hxgrains_`counter'=h`i'
gen hqgrains_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen ngrainstot=`counter'-1

local counter=1
gen npulses=0
gen nhpulses=0
gen xpulses=0
gen hxpulses=0

foreach i of varlist x101-x119{
replace npulses=npulses+1 if `i'>0
replace nhpulses=nhpulses+1 if h`i'>0
replace xpulses=xpulses+`i'
replace hxpulses=hxpulses+h`i'

gen xpulses_`counter'=`i'
gen qpulses_`counter'=q`i'
gen hxpulses_`counter'=h`i'
gen hqpulses_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen npulsestot=`counter'-1

local counter=1
gen nmilk=0
gen nhmilk=0
gen xmilk=0
gen hxmilk=0

foreach i of varlist x121-x129{
replace nmilk=nmilk+1 if `i'>0
replace nhmilk=nhmilk+1 if h`i'>0
replace xmilk=xmilk+`i'
replace hxmilk=hxmilk+h`i'

gen xmilk_`counter'=`i'
gen qmilk_`counter'=q`i'
gen hxmilk_`counter'=h`i'
gen hqmilk_`counter'=hq`i'



local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmilktot=`counter'-1

local counter=1
gen noil=0
gen nhoil=0
gen xoil=0
gen hxoil=0

foreach i of varlist x131-x149{
replace noil=noil+1 if `i'>0
replace nhoil=nhoil+1 if h`i'>0
replace xoil=xoil+`i'
replace hxoil=hxoil+h`i'

gen xoil_`counter'=`i'
gen qoil_`counter'=q`i'
gen hxoil_`counter'=h`i'
gen hqoil_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen noiltot=`counter'-1

local counter=1
gen nmeat=0
gen nhmeat=0
gen xmeat=0
gen hxmeat=0


foreach i of varlist x151-x182{
replace nmeat=nmeat+1 if `i'>0
replace nhmeat=nhmeat+1 if h`i'>0
replace xmeat=xmeat+`i'
replace hxmeat=hxmeat+h`i'


gen xmeat_`counter'=`i'
gen qmeat_`counter'=q`i'
gen hxmeat_`counter'=h`i'
gen hqmeat_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmeattot=`counter'-1

local counter=1
gen nveg=0
gen nhveg=0
gen xveg=0
gen hxveg=0

foreach i of varlist x201-x239{
replace nveg=nveg+1 if `i'>0
replace nhveg=nhveg+1 if h`i'>0
replace xveg=xveg+`i'
replace hxveg=hxveg+h`i'

gen xveg_`counter'=`i'
gen qveg_`counter'=q`i'
gen hxveg_`counter'=h`i'
gen hqveg_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nvegtot=`counter'-1

local counter=1
gen nfru=0
gen nhfru=0
gen xfru=0
gen hxfru=0

foreach i of varlist x241-x269{
replace nfru=nfru+1 if `i'>0
replace nhfru=nhfru+1 if h`i'>0
replace xfru=xfru+`i'
replace hxfru=hxfru+h`i'

gen xfru_`counter'=`i'
gen qfru_`counter'=q`i'
gen hxfru_`counter'=h`i'
gen hqfru_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nfrutot=`counter'-1

local counter=1
gen nsugar=0
gen nhsugar=0
gen xsugar=0
gen hxsugar=0


foreach i of varlist x271-x299{
replace nsugar=nsugar+1 if `i'>0
replace nhsugar=nhsugar+1 if h`i'>0
replace xsugar=xsugar+`i'
replace hxsugar=hxsugar+h`i'

gen xsugar_`counter'=`i'
gen qsugar_`counter'=q`i'
gen hxsugar_`counter'=h`i'
gen hqsugar_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nsugartot=`counter'-1

local counter=1
gen nbev=0
gen nhbev=0
gen xbev=0
gen hxbev=0

foreach i of varlist x301-x309{
replace nbev=nbev+1 if `i'>0
replace nhbev=nhbev+1 if h`i'>0
replace xbev=xbev+`i'
replace hxbev=hxbev+h`i'

gen xbev_`counter'=`i'
gen qbev_`counter'=q`i'
gen hxbev_`counter'=h`i'
gen hqbev_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nbevtot=`counter'-1

local counter=1
gen nproc=0
gen nhproc=0
gen xproc=0
gen hxproc=0

foreach i of varlist x311-x319{
replace nproc=nproc+1 if `i'>0
replace nhproc=nhproc+1 if h`i'>0
replace xproc=xproc+`i'
replace hxproc=hxproc+h`i'

gen xproc_`counter'=`i'
gen qproc_`counter'=q`i'
gen hxproc_`counter'=h`i'
gen hqproc_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nproctot=`counter'-1

local counter=1
gen nintox=0
gen nhintox=0
gen xintox=0
gen hxintox=0

foreach i of varlist x401-x429{
replace nintox=nintox+1 if `i'>0
replace nhintox=nhintox+1 if h`i'>0
replace xintox=xintox+`i'
replace hxintox=hxintox+h`i'

gen xintox_`counter'=`i'
gen qintox_`counter'=q`i'
gen hxintox_`counter'=h`i'
gen hqintox_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nintoxtot=`counter'-1



local counter=1
gen nlight=0
gen nhlight=0
gen xlight=0
gen hxlight=0
foreach i of varlist x501-x519{
replace nlight=nlight+1 if `i'>0
replace nhlight=nhlight+1 if h`i'>0
replace xlight=xlight+`i'
replace hxlight=hxlight+h`i'
gen xlight_`counter'=`i'
gen qlight_`counter'=q`i'
gen hxlight_`counter'=h`i'
gen hqlight_`counter'=hq`i'
local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nlighttot=`counter'-1

local counter=1
gen ncloth=0
gen nhcloth=0
gen xcloth=0
gen hxcloth=0
foreach i of varlist x601-x649{
replace ncloth=ncloth+1 if `i'>0
replace nhcloth=nhcloth+1 if h`i'>0
replace xcloth=xcloth+`i'
replace hxcloth=hxcloth+h`i'
gen xcloth_`counter'=`i'
gen qcloth_`counter'=q`i'
gen hxcloth_`counter'=h`i'
gen hqcloth_`counter'=hq`i'
local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nclothtot=`counter'-1


aorder
local counter=1
gen nmisc=0
gen xmisc=0
foreach i of varlist x701-x820 x1031-x1040{
replace nmisc=nmisc+1 if `i'>0
replace xmisc=xmisc+`i'
local counter=`counter'+1
}
gen nmisctot=`counter'-1

local counter=1
gen ndur=0
gen xdur=0
foreach i of varlist x901-x1019{
replace ndur=ndur+1 if `i'>0
replace xdur=xdur+`i'
local counter=`counter'+1
drop `i'
}
gen ndurtot=`counter'-1

*including 1050 for now...
gen xmed=0
foreach j of varlist x711-x719 x1050{
replace `j'=0 if `j'==.
replace xmed=xmed+`j'
drop `j'
}

gen xeduc=0
foreach i of varlist x705 x706 x1031 x1032 x1033 x1034 x1039{
replace xeduc=xeduc+`i'
drop `i'
}

local counter=1
gen xent=0
gen nent=0
foreach i of varlist x701 x702 x703 x704 x707 x709 x1040{
replace xent=xent+`i'
replace nent=nent+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nenttot=`counter'-1

gen xtrans=0
foreach i of varlist x771-x789{
replace xtrans=xtrans+`i'
drop `i'
}

local counter=1
gen xser=0
gen nser=0
foreach i of varlist x761 x762 x763 x764 x765 x766 x767 x768 x769 x791{
replace xser=xser+`i'
replace nser=nser+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nsertot=`counter'-1

local counter=1
gen xnd=0
gen nnd=0
foreach i of varlist x721-x759{
replace xnd=xnd+`i'
replace nnd=nnd+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nndtot=`counter'-1

gen xactrent=x811+x812+x813
drop x811 x812 x813
gen xtax=x820
drop x820

gen ximprent=.

cap: drop qx*
cap: drop hqx*


summ n*tot
***use individual prices, individual prices adjusted for income, median village/district/region prices

aorder
save round38_test, replace


*****************Round 43***********
clear
use round43_wide_edit
gen round=43
aorder

foreach i of varlist q1-q418 hq1-hq418{
replace `i'=0 if `i'==.
}

foreach i of varlist x1-x688 hx1-hx418{
replace `i'=0 if `i'==.
}



gen xservants=x504

***Drop or merge goods if necessary


replace x270=x270+x271
replace q270=q270+q271
drop x271 q271


replace hx270=hx270+hx271
replace hq270=hq270+hq271
drop hx271 hq271




replace x378=x378+x372+x374+x367
replace q378=0
drop x372 x374 x367 q372 q374 q367

replace x418=x418+x413
replace q418=q418+q413
drop x413 q413


replace hx378=hx378+hx372+hx374+hx367
replace hq378=0
drop hx372 hx374 hx367 hq372 hq374 hq367

replace hx418=hx418+hx413
replace hq418=hq418+hq413
drop hx413 hq413

replace x338=x332+x333+x334+x338
replace q338=q332+q333+q334+q338
drop x332 x333 x334 q332 q333 q334

replace hx338=hx332+hx333+hx334+hx338
replace hq338=hq332+hq333+hq334+hq338
drop hx332 hx333 hx334 hq332 hq333 hq334


replace x428=x428+x426
drop x426

replace x433=x433+x452
drop x452

replace x441=x441+x442+x443
drop x442 x443

replace x458=x458+x455
drop x455

replace x465=x465+x466+x467
drop x466 x467

replace x448=x448+x501
drop x501

ren x502 x445

replace x477=x477+x480
drop x480

replace x518=x518+x512
drop x512

replace x538=x538+x530+x531+x535
drop x530 x531 x535

***
replace x571=x571+x572
drop x572

replace x573=x573+x575+x576
drop x575 x576

replace x588=x588+x574
drop x574

replace x592=x591+x592
drop x591

replace x600=x600+x601
drop x601

replace x602=x602+x597+x596
drop x596 x597

replace x621=x621+x622+x623+x624+x625+x626
drop x622 x623 x624 x625 x626

replace x628=x628+x627
drop x627

replace x658=x658+x654
drop x654

replace x660=x660+x661
drop x661

replace x668=x668+x662+x663
drop x662 x663

replace x678=x678+x671+x672
drop x671 x672


***Drop unnecessary quantities
renpfix q qx
renpfix hq hqx



aorder
gen ngrains=0
gen nhgrains=0
gen xgrains=0
gen hxgrains=0

local counter=1
foreach i of varlist x1-x108{
replace ngrains=ngrains+1 if `i'>0
replace nhgrains=nhgrains+1 if h`i'>0
replace xgrains=xgrains+`i'
replace hxgrains=hxgrains+h`i'

gen xgrains_`counter'=`i'
gen qgrains_`counter'=q`i'
gen hxgrains_`counter'=h`i'
gen hqgrains_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen ngrainstot=`counter'-1

local counter=1
gen npulses=0
gen nhpulses=0
gen xpulses=0
gen hxpulses=0

foreach i of varlist x110-x128{
replace npulses=npulses+1 if `i'>0
replace nhpulses=nhpulses+1 if h`i'>0
replace xpulses=xpulses+`i'
replace hxpulses=hxpulses+h`i'

gen xpulses_`counter'=`i'
gen qpulses_`counter'=q`i'
gen hxpulses_`counter'=h`i'
gen hqpulses_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen npulsestot=`counter'-1

local counter=1
gen nmilk=0
gen nhmilk=0
gen xmilk=0
gen hxmilk=0

foreach i of varlist x130-x138{
replace nmilk=nmilk+1 if `i'>0
replace nhmilk=nhmilk+1 if h`i'>0
replace xmilk=xmilk+`i'
replace hxmilk=hxmilk+h`i'

gen xmilk_`counter'=`i'
gen qmilk_`counter'=q`i'
gen hxmilk_`counter'=h`i'
gen hqmilk_`counter'=hq`i'



local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmilktot=`counter'-1

local counter=1
gen noil=0
gen nhoil=0
gen xoil=0
gen hxoil=0


foreach i of varlist x140-x158{
replace noil=noil+1 if `i'>0
replace nhoil=nhoil+1 if h`i'>0
replace xoil=xoil+`i'
replace hxoil=hxoil+h`i'

gen xoil_`counter'=`i'
gen qoil_`counter'=q`i'
gen hxoil_`counter'=h`i'
gen hqoil_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen noiltot=`counter'-1

local counter=1
gen nmeat=0
gen nhmeat=0
gen xmeat=0
gen hxmeat=0


foreach i of varlist x160-x175{
replace nmeat=nmeat+1 if `i'>0
replace nhmeat=nhmeat+1 if h`i'>0
replace xmeat=xmeat+`i'
replace hxmeat=hxmeat+h`i'

gen xmeat_`counter'=`i'
gen qmeat_`counter'=q`i'
gen hxmeat_`counter'=h`i'
gen hqmeat_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmeattot=`counter'-1

local counter=1
gen nveg=0
gen nhveg=0
gen xveg=0
gen hxveg=0

foreach i of varlist x180-x218{
replace nveg=nveg+1 if `i'>0
replace nhveg=nhveg+1 if h`i'>0
replace xveg=xveg+`i'
replace hxveg=hxveg+h`i'

gen xveg_`counter'=`i'
gen qveg_`counter'=q`i'
gen hxveg_`counter'=h`i'
gen hqveg_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nvegtot=`counter'-1

local counter=1
gen nfru=0
gen nhfru=0
gen xfru=0
gen hxfru=0

foreach i of varlist x230-x258{
replace nfru=nfru+1 if `i'>0
replace nhfru=nhfru+1 if `i'>0
replace xfru=xfru+`i'
replace hxfru=hxfru+h`i'

gen xfru_`counter'=`i'
gen qfru_`counter'=q`i'
gen hxfru_`counter'=h`i'
gen hqfru_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nfrutot=`counter'-1

local counter=1
gen nsugar=0
gen nhsugar=0
gen xsugar=0
gen hxsugar=0

foreach i of varlist x260-x288{
replace nsugar=nsugar+1 if `i'>0
replace nhsugar=nhsugar+1 if h`i'>0
replace xsugar=xsugar+`i'
replace hxsugar=hxsugar+h`i'

gen xsugar_`counter'=`i'
gen qsugar_`counter'=q`i'
gen hxsugar_`counter'=h`i'
gen hqsugar_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nsugartot=`counter'-1

local counter=1
gen nbev=0
gen nhbev=0
gen xbev=0
gen hxbev=0

foreach i of varlist x290-x308{
replace nbev=nbev+1 if `i'>0
replace nhbev=nhbev+1 if h`i'>0
replace xbev=xbev+`i'
replace hxbev=hxbev+h`i'

gen xbev_`counter'=`i'
gen qbev_`counter'=q`i'
gen hxbev_`counter'=h`i'
gen hqbev_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nbevtot=`counter'-1

local counter=1
gen nproc=0
gen nhproc=0
gen xproc=0
gen hxproc=0

foreach i of varlist x310-x318{
replace nproc=nproc+1 if `i'>0
replace nhproc=nhproc+1 if h`i'>0
replace xproc=xproc+`i'
replace hxproc=hxproc+h`i'

gen xproc_`counter'=`i'
gen qproc_`counter'=q`i'
gen hxproc_`counter'=h`i'
gen hqproc_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nproctot=`counter'-1

local counter=1
gen nintox=0
gen nhintox=0
gen xintox=0
gen hxintox=0

foreach i of varlist x330-x358{
replace nintox=nintox+1 if `i'>0
replace nhintox=nhintox+1 if h`i'>0
replace xintox=xintox+`i'
replace hxintox=hxintox+h`i'

gen xintox_`counter'=`i'
gen qintox_`counter'=q`i'
gen hxintox_`counter'=h`i'
gen hqintox_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nintoxtot=`counter'-1


local counter=1
gen ncloth=0
gen nhcloth=0
gen xcloth=0
gen hxcloth=0
foreach i of varlist x380-x418{
replace ncloth=ncloth+1 if `i'>0
replace nhcloth=nhcloth+1 if h`i'>0
replace xcloth=xcloth+`i'
replace hxcloth=hxcloth+h`i'
gen xcloth_`counter'=`i'
gen qcloth_`counter'=q`i'
gen hxcloth_`counter'=h`i'
gen hqcloth_`counter'=hq`i'
local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nclothtot=`counter'-1


local counter=1
gen nlight=0
gen nhlight=0
gen xlight=0
gen hxlight=0
foreach i of varlist x360-x378{
replace nlight=nlight+1 if `i'>0
replace nhlight=nhlight+1 if h`i'>0
replace xlight=xlight+`i'
replace hxlight=hxlight+h`i'
gen xlight_`counter'=`i'
gen qlight_`counter'=q`i'
gen hxlight_`counter'=h`i'
gen hqlight_`counter'=hq`i'
local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nlighttot=`counter'-1


local counter=1
gen nmisc=0
gen xmisc=0
foreach i of varlist x420-x552{
replace nmisc=nmisc+1 if `i'>0
replace xmisc=xmisc+`i'
local counter=`counter'+1
}
gen nmisctot=`counter'-1

local counter=1
gen ndur=0
gen xdur=0
foreach i of varlist x570-x688{
replace ndur=ndur+1 if `i'>0
replace xdur=xdur+`i'
local counter=`counter'+1
drop `i'
}
gen ndurtot=`counter'-1

gen xmed=0
foreach j of varlist x440-x448 x500{
replace `j'=0 if `j'==.
replace xmed=xmed+`j'
drop `j'
}

gen xeduc=0
foreach i of varlist x430-x438 x503{
replace xeduc=xeduc+`i'
drop `i'
}

local counter=1
gen xent=0
gen nent=0
foreach i of varlist x420-x428{
replace xent=xent+`i'
replace nent=nent+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nenttot=`counter'-1

gen xtrans=0
foreach i of varlist x520-x538{
replace xtrans=xtrans+`i'
drop `i'
}

local counter=1
gen xser=0
gen nser=0
foreach i of varlist x504-x518 x540{
replace xser=xser+`i'
replace nser=nser+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nsertot=`counter'-1

local counter=1
gen xnd=0
gen nnd=0
foreach i of varlist x450-x488{
replace xnd=xnd+`i'
replace nnd=nnd+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nndtot=`counter'-1


gen xactrent=x550+x551+x552
drop x550 x551 x552

*note- the tax variable is missing in this survey
gen xtax=.
gen ximprent=.

cap: drop qx*
cap: drop hqx*


summ n*tot
****************
aorder
save round43_test, replace









****************************************************************
*****************************
*********Round 55***********
*****************************

foreach round in 55{
use round`round'_wide_edit, clear
gen round=55
aorder

*include clothes for quantities, but not for home production
foreach i of varlist q1-q294 hq1-hq253 hx1-hx253{
replace `i'=0 if `i'==.
}

foreach i of varlist x1-x543{
replace `i'=0 if `i'==.
}


gen xservants=x380

gen pds_xrice=x1
gen pds_qrice=q1
gen mkt_xrice=x2
gen mkt_qrice=q2
gen home_xrice=hx2
gen home_qrice=hq2

gen pds_xwheat=x7
gen pds_qwheat=q7
gen mkt_xwheat=x8
gen mkt_qwheat=q8
gen home_xwheat=hx8
gen home_qwheat=hq8


gen pds_xsugar=x160
gen pds_qsugar=q160
gen mkt_xsugar=x161
gen mkt_qsugar=q161
gen home_xsugar=hx161
gen home_qsugar=hq161


***merge goods***





replace x1=x1+x2
replace q1=q1+q2
drop x2 q2
replace x7=x7+x8
replace q7=q7+q8
drop x8 q8

replace x147=x147+x138
replace q147=q147+q138
drop q138 x138

replace x160=x160+x161
replace q160=q160+q161
drop q161 x161

replace x122=x122+x106
replace q122=q122+q106
drop x106 q106


replace hx122=hx122+hx106
replace hq122=hq122+hq106
drop hx106 hq106



replace x215=x212+x213+x214+x215
replace q215=q212+q213+q214+q215
drop x212 x213 x214 q212 q213 q214

replace hx215=hx212+hx213+hx214+hx215
replace hq215=hq212+hq213+hq214+hq215
drop hx212 hx213 hx214 hq212 hq213 hq214


replace x244=x244+x245
replace q244=q244+q245
drop x245 q245

replace hx244=hx244+hx245
replace hq244=hq244+hq245
drop hx245 hq245


gen x288=x287+x273+x274
gen q288=0
drop x273 x274 x287 q273 q274 q287

gen x287=x272
gen q287=q272
drop x272 q272

replace x294=x293+x294
replace q294=q293+q294
drop x293 q293


replace x303=x303+x342
drop x342

replace x324=x324+x311+x321+x314
drop x311 x321 x314

replace x337=x337+x336
drop x336

replace x312=x312+x322
drop x322

replace x355=x355+x356
drop x356

replace x358=x358+x357
drop x357

replace x373=x373+x372+x371
drop x371 x372

replace x387=x387+x388
drop x388

replace x394=x394+x391+x392+x386
drop x391 x392 x386


replace x441=x440+x441
drop x440
*****

replace x462=x462+x463
drop x463

replace x482=x482+x483
drop x483

replace x531=x531+x532
drop x532

replace x542=x542+x541+x540
drop x540 x541

******Repeat h goods



***Drop and merge goods***


replace hx1=hx1+hx2
replace hq1=hq1+hq2
drop hx2 hq2
replace hx7=hx7+hx8
replace hq7=hq7+hq8
drop hx8 hq8

replace hx147=hx147+hx138
replace hq147=hq147+hq138
drop hq138 hx138

replace hx160=hx160+hx161
replace hq160=hq160+hq161
drop hq161 hx161




aorder


renpfix q qx
renpfix hq hqx



aorder
gen ngrains=0
gen nhgrains=0
gen xgrains=0
gen hxgrains=0

local counter=1
foreach i of varlist x1-x39{
replace ngrains=ngrains+1 if `i'>0
replace nhgrains=nhgrains+1 if h`i'>0
replace xgrains=xgrains+`i'
replace hxgrains=hxgrains+h`i'

gen xgrains_`counter'=`i'
gen qgrains_`counter'=q`i'
gen hxgrains_`counter'=h`i'
gen hqgrains_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen ngrainstot=`counter'-1

local counter=1
gen npulses=0
gen nhpulses=0
gen xpulses=0
gen hxpulses=0


foreach i of varlist x40-x53{
replace npulses=npulses+1 if `i'>0
replace nhpulses=nhpulses+1 if h`i'>0
replace xpulses=xpulses+`i'
replace hxpulses=hxpulses+h`i'

gen xpulses_`counter'=`i'
gen qpulses_`counter'=q`i'
gen hxpulses_`counter'=h`i'
gen hqpulses_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen npulsestot=`counter'-1

local counter=1
gen nmilk=0
gen nhmilk=0
gen xmilk=0
gen hxmilk=0

foreach i of varlist x60-x67{
replace nmilk=nmilk+1 if `i'>0
replace nhmilk=nhmilk+1 if h`i'>0
replace xmilk=xmilk+`i'
replace hxmilk=hxmilk+h`i'

gen xmilk_`counter'=`i'
gen qmilk_`counter'=q`i'
gen hxmilk_`counter'=h`i'
gen hqmilk_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmilktot=`counter'-1

local counter=1
gen noil=0
gen nhoil=0
gen xoil=0
gen hxoil=0


foreach i of varlist x70-x74{
replace noil=noil+1 if `i'>0
replace nhoil=nhoil+1 if h`i'>0
replace xoil=xoil+`i'
replace hxoil=hxoil+h`i'

gen xoil_`counter'=`i'
gen qoil_`counter'=q`i'
gen hxoil_`counter'=h`i'
gen hqoil_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen noiltot=`counter'-1

local counter=1
gen nmeat=0
gen nhmeat=0
gen xmeat=0
gen hxmeat=0



foreach i of varlist x80-x86{
replace nmeat=nmeat+1 if `i'>0
replace nhmeat=nhmeat+1 if h`i'>0
replace xmeat=xmeat+`i'
replace hxmeat=hxmeat+h`i'


gen xmeat_`counter'=`i'
gen qmeat_`counter'=q`i'
gen hxmeat_`counter'=h`i'
gen hqmeat_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmeattot=`counter'-1

local counter=1
gen nveg=0
gen nhveg=0
gen xveg=0
gen hxveg=0


foreach i of varlist x90-x122{
replace nveg=nveg+1 if `i'>0
replace nhveg=nhveg+1 if h`i'>0
replace xveg=xveg+`i'
replace hxveg=hxveg+h`i'

gen xveg_`counter'=`i'
gen qveg_`counter'=q`i'
gen hxveg_`counter'=h`i'
gen hqveg_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nvegtot=`counter'-1

local counter=1
gen nfru=0
gen nhfru=0
gen xfru=0
gen hxfru=0


foreach i of varlist x130-x157{
replace nfru=nfru+1 if `i'>0
replace nhfru=nhfru+1 if h`i'>0
replace xfru=xfru+`i'
replace hxfru=hxfru+h`i'

gen xfru_`counter'=`i'
gen qfru_`counter'=q`i'
gen hxfru_`counter'=h`i'
gen hqfru_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nfrutot=`counter'-1

local counter=1
gen nsugar=0
gen nhsugar=0
gen xsugar=0
gen hxsugar=0


foreach i of varlist x160-x188{
replace nsugar=nsugar+1 if `i'>0
replace nhsugar=nhsugar+1 if h`i'>0
replace xsugar=xsugar+`i'
replace hxsugar=hxsugar+h`i'


gen xsugar_`counter'=`i'
gen qsugar_`counter'=q`i'
gen hxsugar_`counter'=h`i'
gen hqsugar_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nsugartot=`counter'-1

local counter=1
gen nbev=0
gen nhbev=0
gen xbev=0
gen hxbev=0

foreach i of varlist x190-x198{
replace nbev=nbev+1 if `i'>0
replace nhbev=nhbev+1 if h`i'>0
replace xbev=xbev+`i'
replace hxbev=hxbev+h`i'

gen xbev_`counter'=`i'
gen qbev_`counter'=q`i'
gen hxbev_`counter'=h`i'
gen hqbev_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nbevtot=`counter'-1

local counter=1
gen nproc=0
gen nhproc=0
gen xproc=0
gen hxproc=0


foreach i of varlist x200-x208{
replace nproc=nproc+1 if `i'>0
replace nhproc=nhproc+1 if h`i'>0
replace xproc=xproc+`i'
replace hxproc=hxproc+h`i'

gen xproc_`counter'=`i'
gen qproc_`counter'=q`i'
gen hxproc_`counter'=h`i'
gen hqproc_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nproctot=`counter'-1

local counter=1
gen nintox=0
gen nhintox=0
gen xintox=0
gen hxintox=0


foreach i of varlist x210-x235{
replace nintox=nintox+1 if `i'>0
replace nhintox=nhintox+1 if h`i'>0
replace xintox=xintox+`i'
replace hxintox=hxintox+h`i'

gen xintox_`counter'=`i'
gen qintox_`counter'=q`i'
gen hxintox_`counter'=h`i'
gen hqintox_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nintoxtot=`counter'-1


local counter=1
gen nlight=0
gen nhlight=0
gen xlight=0
gen hxlight=0
foreach i of varlist x240-x253{
replace nlight=nlight+1 if `i'>0
replace nhlight=nhlight+1 if h`i'>0
replace xlight=xlight+`i'
replace hxlight=hxlight+h`i'
gen xlight_`counter'=`i'
gen qlight_`counter'=q`i'
gen hxlight_`counter'=h`i'
gen hqlight_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nlighttot=`counter'-1




local counter=1
gen ncloth=0
gen xcloth=0
foreach i of varlist x260-x294{
replace ncloth=ncloth+1 if `i'>0
replace xcloth=xcloth+`i'
gen xcloth_`counter'=`i'
gen qcloth_`counter'=q`i'
local counter=`counter'+1
drop `i' q`i'
}
gen nclothtot=`counter'-1


local counter=1
gen nmisc=0
gen xmisc=0
foreach i of varlist x300-x441{
replace nmisc=nmisc+1 if `i'>0
replace xmisc=xmisc+`i'
local counter=`counter'+1
}
gen nmisctot=`counter'-1

local counter=1
gen ndur=0
gen xdur=0
foreach i of varlist x450-x543{
replace ndur=ndur+1 if `i'>0
replace xdur=xdur+`i'
local counter=`counter'+1
drop `i'
}
gen ndurtot=`counter'-1


aorder

gen xmed=0
foreach j of varlist x310-x313 x320-x324{
replace `j'=0 if `j'==.
replace xmed=xmed+`j'
drop `j'
}

gen xeduc=0
foreach i of varlist x300-x306{
replace xeduc=xeduc+`i'
drop `i'
}

local counter=1
gen xent=0
gen nent=0
foreach i of varlist x330-x337{
replace xent=xent+`i'
replace nent=nent+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nenttot=`counter'-1

gen xtrans=0
foreach i of varlist x400-x413{
replace xtrans=xtrans+`i'
drop `i'
}

local counter=1
gen xser=0
gen nser=0
foreach i of varlist x380-x394{
replace xser=xser+`i'
replace nser=nser+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nsertot=`counter'-1

local counter=1
gen xnd=0
gen nnd=0
foreach i of varlist x340-x373{
replace xnd=xnd+`i'
replace nnd=nnd+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nndtot=`counter'-1

gen xactrent=x420+x421+x422
drop x420 x421 x422

gen ximprent=x439
drop x439

gen xtax=x441
drop x441




cap: drop qx*
cap: drop hqx*

summ n*tot

aorder
save round`round'_test, replace
}

***************************************


clear
use round61_wide_edit
gen round=61
aorder


foreach i of varlist q1-q294 hq1-hq253 hx1-hx253{
replace `i'=0 if `i'==.
}

foreach i of varlist x1-x543{
replace `i'=0 if `i'==.
}


gen xservants=x380


gen pds_xrice=x1
gen pds_qrice=q1
gen mkt_xrice=x2
gen mkt_qrice=q2
gen home_xrice=hx2
gen home_qrice=hq2


gen pds_xwheat=x7
gen pds_qwheat=q7
gen mkt_xwheat=x8
gen mkt_qwheat=q8
gen home_xwheat=hx8
gen home_qwheat=hq8


gen pds_xsugar=x160
gen pds_qsugar=q160
gen mkt_xsugar=x161
gen mkt_qsugar=q161
gen home_xsugar=hx161
gen home_qsugar=hq161





replace x1=x1+x2
replace q1=q1+q2
drop x2 q2
replace x7=x7+x8
replace q7=q7+q8
drop x8 q8


replace x39 = x39 + x22
replace q39 = q39 +q22
drop x22 q22

*note 122 is now other veg., 124 no longer exists
replace x122=x106+x122
replace q122=q106+q122
drop x106 q106


replace x147=x147+x138
replace q147=q147+q138
drop q138 x138

replace x160=x160+x161
replace q160=q160+q161
drop q161 x161
 


replace hx39 = hx39 + hx22
replace hq39 = hq39 +hq22
drop hx22 hq22

*note 122 is now other veg., 124 no longer exists
replace hx122=hx106+hx122
replace hq122=hq106+hq122
drop hx106 hq106

 
 
 
replace x215=x212+x213+x214+x215
replace q215=q212+q213+q214+q215
drop x212 x213 x214 q212 q213 q214

replace hx215=hx212+hx213+hx214+hx215
replace hq215=hq212+hq213+hq214+hq215
drop hx212 hx213 hx214 hq212 hq213 hq214


replace x244=x244+x245
replace q244=q244+q245
drop x245 q245


replace hx244=hx244+hx245
replace hq244=hq244+hq245
drop hx245 hq245





gen x288=x287+x273+x274
gen q288=0
drop x273 x274 x287 q273 q274 q287

gen x287=x272
gen q287=q272
drop x272 q272

replace x294=x293+x294
replace q294=q293+q294
drop x293 q293
 

replace x324=x324+x311+x314+x321
drop x311 x314 x321

replace x312=x312+x322
drop x322

replace x338=x337+x336+x338
drop x336 x337

replace x355=x355+x356
drop x356

replace x358=x358+x357
drop x357

replace x373=x373+x372+x371
drop x371 x372

replace x387=x387+x388
drop x388

replace x394=x394+x391+x392+x386
drop x391 x392 x386

replace x441=x440+x441
drop x440

replace x462=x462+x463
drop x463

replace x482=x482+x483
drop x483

replace x491=x491+x492
drop x492

replace x531=x531+x532+x533+x534
drop x532 x533 x534 


replace x542=x542+x541+x540
drop x540 x541
 
 
 

replace hx1=hx1+hx2
replace hq1=hq1+hq2
drop hx2 hq2
replace hx7=hx7+hx8
replace hq7=hq7+hq8
drop hx8 hq8


replace hx147=hx147+hx138
replace hq147=hq147+hq138
drop hq138 hx138

replace hx160=hx160+hx161
replace hq160=hq160+hq161
drop hq161 hx161
 

 
 

aorder


renpfix q qx
renpfix hq hqx



aorder
gen ngrains=0
gen nhgrains=0
gen xgrains=0
gen hxgrains=0


local counter=1
foreach i of varlist x1-x39{
replace ngrains=ngrains+1 if `i'>0
replace nhgrains=nhgrains+1 if h`i'>0
replace xgrains=xgrains+`i'
replace hxgrains=hxgrains+h`i'


gen xgrains_`counter'=`i'
gen qgrains_`counter'=q`i'
gen hxgrains_`counter'=h`i'
gen hqgrains_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen ngrainstot=`counter'-1

local counter=1
gen npulses=0
gen nhpulses=0
gen xpulses=0
gen hxpulses=0

foreach i of varlist x40-x53{
replace npulses=npulses+1 if `i'>0
replace nhpulses=nhpulses+1 if h`i'>0
replace xpulses=xpulses+`i'
replace hxpulses=hxpulses+h`i'

gen xpulses_`counter'=`i'
gen qpulses_`counter'=q`i'
gen hxpulses_`counter'=h`i'
gen hqpulses_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen npulsestot=`counter'-1

local counter=1
gen nmilk=0
gen nhmilk=0
gen xmilk=0
gen hxmilk=0

foreach i of varlist x60-x67{
replace nmilk=nmilk+1 if `i'>0
replace nhmilk=nhmilk+1 if h`i'>0
replace xmilk=xmilk+`i'
replace hxmilk=hxmilk+h`i'

gen xmilk_`counter'=`i'
gen qmilk_`counter'=q`i'
gen hxmilk_`counter'=h`i'
gen hqmilk_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmilktot=`counter'-1

local counter=1
gen noil=0
gen nhoil=0
gen xoil=0
gen hxoil=0

foreach i of varlist x70-x74{
replace noil=noil+1 if `i'>0
replace nhoil=nhoil+1 if h`i'>0
replace xoil=xoil+`i'
replace hxoil=hxoil+h`i'

gen xoil_`counter'=`i'
gen qoil_`counter'=q`i'
gen hxoil_`counter'=h`i'
gen hqoil_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen noiltot=`counter'-1

local counter=1
gen nmeat=0
gen nhmeat=0
gen xmeat=0
gen hxmeat=0


foreach i of varlist x80-x86{
replace nmeat=nmeat+1 if `i'>0
replace nhmeat=nhmeat+1 if h`i'>0
replace xmeat=xmeat+`i'
replace hxmeat=hxmeat+h`i'


gen xmeat_`counter'=`i'
gen qmeat_`counter'=q`i'
gen hxmeat_`counter'=h`i'
gen hqmeat_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmeattot=`counter'-1

local counter=1
gen nveg=0
gen nhveg=0
gen xveg=0
gen hxveg=0


foreach i of varlist x90-x122{
replace nveg=nveg+1 if `i'>0
replace nhveg=nhveg+1 if h`i'>0
replace xveg=xveg+`i'
replace hxveg=hxveg+h`i'

gen xveg_`counter'=`i'
gen qveg_`counter'=q`i'
gen hxveg_`counter'=h`i'
gen hqveg_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nvegtot=`counter'-1

local counter=1
gen nfru=0
gen nhfru=0
gen xfru=0
gen hxfru=0


foreach i of varlist x130-x157{
replace nfru=nfru+1 if `i'>0
replace nhfru=nhfru+1 if h`i'>0
replace xfru=xfru+`i'
replace hxfru=hxfru+h`i'


gen xfru_`counter'=`i'
gen qfru_`counter'=q`i'
gen hxfru_`counter'=h`i'
gen hqfru_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nfrutot=`counter'-1

local counter=1
gen nsugar=0
gen nhsugar=0
gen xsugar=0
gen hxsugar=0


foreach i of varlist x160-x188{
replace nsugar=nsugar+1 if `i'>0
replace nhsugar=nhsugar+1 if h`i'>0
replace xsugar=xsugar+`i'
replace hxsugar=hxsugar+h`i'


gen xsugar_`counter'=`i'
gen qsugar_`counter'=q`i'
gen hxsugar_`counter'=h`i'
gen hqsugar_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nsugartot=`counter'-1

local counter=1
gen nbev=0
gen nhbev=0
gen xbev=0
gen hxbev=0

foreach i of varlist x190-x198{
replace nbev=nbev+1 if `i'>0
replace nhbev=nhbev+1 if `i'>0
replace xbev=xbev+`i'
replace hxbev=hxbev+h`i'

gen xbev_`counter'=`i'
gen qbev_`counter'=q`i'
gen hxbev_`counter'=h`i'
gen hqbev_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nbevtot=`counter'-1

local counter=1
gen nproc=0
gen nhproc=0
gen xproc=0
gen hxproc=0


foreach i of varlist x200-x208{
replace nproc=nproc+1 if `i'>0
replace nhproc=nhproc+1 if h`i'>0
replace xproc=xproc+`i'
replace hxproc=hxproc+h`i'

gen xproc_`counter'=`i'
gen qproc_`counter'=q`i'
gen hxproc_`counter'=h`i'
gen hqproc_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nproctot=`counter'-1

local counter=1
gen nintox=0
gen nhintox=0
gen xintox=0
gen hxintox=0


foreach i of varlist x210-x235{
replace nintox=nintox+1 if `i'>0
replace nhintox=nhintox+1 if `i'>0
replace xintox=xintox+`i'
replace hxintox=hxintox+h`i'

gen xintox_`counter'=`i'
gen qintox_`counter'=q`i'
gen hxintox_`counter'=h`i'
gen hqintox_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nintoxtot=`counter'-1




local counter=1
gen nlight=0
gen nhlight=0
gen xlight=0
gen hxlight=0
foreach i of varlist x240-x253{
replace nlight=nlight+1 if `i'>0
replace nhlight=nhlight+1 if h`i'>0
replace xlight=xlight+`i'
replace hxlight=hxlight+h`i'
gen xlight_`counter'=`i'
gen qlight_`counter'=q`i'
gen hxlight_`counter'=h`i'
gen hqlight_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nlighttot=`counter'-1



local counter=1
gen ncloth=0
gen xcloth=0
foreach i of varlist x260-x294{
replace ncloth=ncloth+1 if `i'>0
replace xcloth=xcloth+`i'
gen xcloth_`counter'=`i'
gen qcloth_`counter'=q`i'
local counter=`counter'+1
drop `i' q`i'
}
gen nclothtot=`counter'-1




local counter=1
gen nmisc=0
gen xmisc=0
foreach i of varlist x300-x441{
replace nmisc=nmisc+1 if `i'>0
replace xmisc=xmisc+`i'
local counter=`counter'+1
}
gen nmisctot=`counter'-1

local counter=1
gen ndur=0
gen xdur=0
foreach i of varlist x450-x543{
replace ndur=ndur+1 if `i'>0
replace xdur=xdur+`i'
local counter=`counter'+1
drop `i'
}
gen ndurtot=`counter'-1

aorder

gen xmed=0
foreach j of varlist x310-x313 x320-x324{
replace `j'=0 if `j'==.
replace xmed=xmed+`j'
drop `j'
}

gen xeduc=0
foreach i of varlist x300-x306{
replace xeduc=xeduc+`i'
drop `i'
}

local counter=1
gen xent=0
gen nent=0
foreach i of varlist x330-x338{
replace xent=xent+`i'
replace nent=nent+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nenttot=`counter'-1

gen xtrans=0
foreach i of varlist x400-x413{
replace xtrans=xtrans+`i'
drop `i'
}

local counter=1
gen xser=0
gen nser=0
foreach i of varlist x380-x394{
replace xser=xser+`i'
replace nser=nser+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nsertot=`counter'-1

local counter=1
gen xnd=0
gen nnd=0
foreach i of varlist x340-x373{
replace xnd=xnd+`i'
replace nnd=nnd+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nndtot=`counter'-1

gen xactrent=x420+x421+x422
drop x420 x421 x422

gen ximprent=x439
drop x439

gen xtax=x441
drop x441

cap: drop qx*
cap: drop hqx*

summ n*tot

aorder
save round61_test, replace




***Round 66
foreach round in 661{
use round`round'_wide_edit, clear
gen round=66

aorder

*note that naturally 66th round goes up to 255 (355-100) for light but these were combined previously
foreach i of varlist q1-q294 hq1-hq253 hx1-hx253{
replace `i'=0 if `i'==.
}

foreach i of varlist x1-x543{
replace `i'=0 if `i'==.
}


gen xservants=x380


gen pds_xrice=x1
gen pds_qrice=q1
gen mkt_xrice=x2
gen mkt_qrice=q2
gen home_xrice=hx2
gen home_qrice=hq2


gen pds_xwheat=x7
gen pds_qwheat=q7
gen mkt_xwheat=x8
gen mkt_qwheat=q8
gen home_xwheat=hx8
gen home_qwheat=hq8


gen pds_xsugar=x160
gen pds_qsugar=q160
gen mkt_xsugar=x161
gen mkt_qsugar=q161
gen home_xsugar=hx161
gen home_qsugar=hq161

gen freemeal_q=qfreemeal
gen freemeal_x=xfreemeal
drop xfreemeal qfreemeal




replace x1=x1+x2
replace q1=q1+q2
drop x2 q2
replace x7=x7+x8
replace q7=q7+q8
drop x8 q8


replace x39 = x39 + x22
replace q39 = q39 + q22
drop x22 q22

replace x122=x106+x122
replace q122=q106+q122
drop x106 q106


replace x147=x147+x138
replace q147=q147+q138
drop q138 x138

replace x160=x160+x161
replace q160=q160+q161
drop q161 x161
 


replace x244=x244+x245
replace q244=q244+q245
drop x245 q245

replace hx244=hx244+hx245
replace hq244=hq244+hq245
drop hx245 hq245



gen x288=x287+x273+x274
drop x273 x274 x287 q273 q274 q287
gen q288=0

gen x287=x272
gen q287=q272
drop x272 q272

replace x294=x293+x294
replace q294=q293+q294
drop x293 q293




replace x324=x324+x311+x314+x321
drop x311 x314 x321

replace x312=x312+x322
drop x322

replace x338=x337+x336+x338
drop x336 x337

replace x355=x355+x356
drop x356

replace x358=x358+x357
drop x357

replace x373=x373+x372+x371
drop x371 x372

replace x387=x387+x388
drop x388

replace x394=x394+x391+x392+x386
drop x391 x392 x386

replace x441=x440+x441
drop x440

replace x462=x462+x463
drop x463

replace x482=x482+x483
drop x483

replace x491=x491+x492
drop x492

replace x531=x531+x532+x533+x534
drop x532 x533 x534 


replace x542=x542+x541+x540
drop x540 x541




replace hx1=hx1+hx2
replace hq1=hq1+hq2
drop hx2 hq2
replace hx7=hx7+hx8
replace hq7=hq7+hq8
drop hx8 hq8

replace hx39 = hx39 + hx22
replace hq39 = hq39 +hq22
drop hx22 hq22

replace hx122=hx106+hx122
replace hq122=hq106+hq122
drop hx106 hq106


replace hx147=hx147+hx138
replace hq147=hq147+hq138
drop hq138 hx138

replace hx160=hx160+hx161
replace hq160=hq160+hq161
drop hq161 hx161
 


aorder


renpfix q qx
renpfix hq hqx



aorder
gen ngrains=0
gen nhgrains=0
gen xgrains=0
gen hxgrains=0


local counter=1
foreach i of varlist x1-x39{
replace ngrains=ngrains+1 if `i'>0
replace nhgrains=nhgrains+1 if h`i'>0
replace xgrains=xgrains+`i'
replace hxgrains=hxgrains+h`i'


gen xgrains_`counter'=`i'
gen qgrains_`counter'=q`i'
gen hxgrains_`counter'=h`i'
gen hqgrains_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen ngrainstot=`counter'-1

local counter=1
gen npulses=0
gen nhpulses=0
gen xpulses=0
gen hxpulses=0

foreach i of varlist x40-x53{
replace npulses=npulses+1 if `i'>0
replace nhpulses=nhpulses+1 if h`i'>0
replace xpulses=xpulses+`i'
replace hxpulses=hxpulses+h`i'

gen xpulses_`counter'=`i'
gen qpulses_`counter'=q`i'
gen hxpulses_`counter'=h`i'
gen hqpulses_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen npulsestot=`counter'-1

local counter=1
gen nmilk=0
gen nhmilk=0
gen xmilk=0
gen hxmilk=0

foreach i of varlist x60-x67{
replace nmilk=nmilk+1 if `i'>0
replace nhmilk=nhmilk+1 if h`i'>0
replace xmilk=xmilk+`i'
replace hxmilk=hxmilk+h`i'

gen xmilk_`counter'=`i'
gen qmilk_`counter'=q`i'
gen hxmilk_`counter'=h`i'
gen hqmilk_`counter'=hq`i'



local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmilktot=`counter'-1

local counter=1
gen noil=0
gen nhoil=0
gen xoil=0
gen hxoil=0

foreach i of varlist x70-x74{
replace noil=noil+1 if `i'>0
replace nhoil=nhoil+1 if h`i'>0
replace xoil=xoil+`i'
replace hxoil=hxoil+h`i'

gen xoil_`counter'=`i'
gen qoil_`counter'=q`i'
gen hxoil_`counter'=h`i'
gen hqoil_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen noiltot=`counter'-1

local counter=1
gen nmeat=0
gen nhmeat=0
gen xmeat=0
gen hxmeat=0


foreach i of varlist x80-x86{
replace nmeat=nmeat+1 if `i'>0
replace nhmeat=nhmeat+1 if h`i'>0
replace xmeat=xmeat+`i'
replace hxmeat=hxmeat+h`i'

gen xmeat_`counter'=`i'
gen qmeat_`counter'=q`i'
gen hxmeat_`counter'=h`i'
gen hqmeat_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nmeattot=`counter'-1

local counter=1
gen nveg=0
gen nhveg=0
gen xveg=0
gen hxveg=0


foreach i of varlist x90-x122{
replace nveg=nveg+1 if `i'>0
replace nhveg=nhveg+1 if h`i'>0
replace xveg=xveg+`i'
replace hxveg=hxveg+h`i'

gen xveg_`counter'=`i'
gen qveg_`counter'=q`i'
gen hxveg_`counter'=h`i'
gen hqveg_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nvegtot=`counter'-1

local counter=1
gen nfru=0
gen nhfru=0
gen xfru=0
gen hxfru=0

foreach i of varlist x130-x157{
replace nfru=nfru+1 if `i'>0
replace nhfru=nhfru+1 if h`i'>0
replace xfru=xfru+`i'
replace hxfru=hxfru+h`i'

gen xfru_`counter'=`i'
gen qfru_`counter'=q`i'
gen hxfru_`counter'=h`i'
gen hqfru_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nfrutot=`counter'-1

local counter=1
gen nsugar=0
gen nhsugar=0
gen xsugar=0
gen hxsugar=0

foreach i of varlist x160-x188{
replace nsugar=nsugar+1 if `i'>0
replace nhsugar=nhsugar+1 if h`i'>0
replace xsugar=xsugar+`i'
replace hxsugar=hxsugar+h`i'


gen xsugar_`counter'=`i'
gen qsugar_`counter'=q`i'
gen hxsugar_`counter'=h`i'
gen hqsugar_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nsugartot=`counter'-1

local counter=1
gen nbev=0
gen nhbev=0
gen xbev=0
gen hxbev=0

foreach i of varlist x190-x198{
replace nbev=nbev+1 if `i'>0
replace nhbev=nhbev+1 if h`i'>0
replace xbev=xbev+`i'
replace hxbev=hxbev+h`i'

gen xbev_`counter'=`i'
gen qbev_`counter'=q`i'
gen hxbev_`counter'=h`i'
gen hqbev_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nbevtot=`counter'-1

local counter=1
gen nproc=0
gen nhproc=0
gen xproc=0
gen hxproc=0

foreach i of varlist x200-x208{
replace nproc=nproc+1 if `i'>0
replace nhproc=nhproc+1 if h`i'>0
replace xproc=xproc+`i'
replace hxproc=hxproc+h`i'

gen xproc_`counter'=`i'
gen qproc_`counter'=q`i'
gen hxproc_`counter'=h`i'
gen hqproc_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nproctot=`counter'-1

local counter=1
gen nintox=0
gen nhintox=0
gen xintox=0
gen hxintox=0

foreach i of varlist x210-x235{
replace nintox=nintox+1 if `i'>0
replace nhintox=nhintox+1 if h`i'>0
replace xintox=xintox+`i'
replace hxintox=hxintox+h`i'

gen xintox_`counter'=`i'
gen qintox_`counter'=q`i'
gen hxintox_`counter'=h`i'
gen hqintox_`counter'=hq`i'


local counter=`counter'+1
drop `i' q`i' h`i' hq`i' 
}
gen nintoxtot=`counter'-1


local counter=1
gen nlight=0
gen nhlight=0
gen xlight=0
gen hxlight=0
foreach i of varlist x240-x253{
replace nlight=nlight+1 if `i'>0
replace nhlight=nhlight+1 if h`i'>0
replace xlight=xlight+`i'
replace hxlight=hxlight+h`i'
gen xlight_`counter'=`i'
gen qlight_`counter'=q`i'
gen hxlight_`counter'=h`i'
gen hqlight_`counter'=hq`i'

local counter=`counter'+1
drop `i' q`i' h`i' hq`i'
}
gen nlighttot=`counter'-1


local counter=1
gen ncloth=0
gen xcloth=0
foreach i of varlist x260-x294{
replace ncloth=ncloth+1 if `i'>0
replace xcloth=xcloth+`i'
gen xcloth_`counter'=`i'
gen qcloth_`counter'=q`i'
local counter=`counter'+1
drop `i' q`i'
}
gen nclothtot=`counter'-1





local counter=1
gen nmisc=0
gen xmisc=0
foreach i of varlist x300-x441{
replace nmisc=nmisc+1 if `i'>0
replace xmisc=xmisc+`i'
local counter=`counter'+1
}
gen nmisctot=`counter'-1

local counter=1
gen ndur=0
gen xdur=0
foreach i of varlist x450-x543{
replace ndur=ndur+1 if `i'>0
replace xdur=xdur+`i'
local counter=`counter'+1
drop `i'
}
gen ndurtot=`counter'-1

aorder

gen xmed=0
foreach j of varlist x310-x313 x320-x324{
replace `j'=0 if `j'==.
replace xmed=xmed+`j'
drop `j'
}

gen xeduc=0
foreach i of varlist x300-x306{
replace xeduc=xeduc+`i'
drop `i'
}

local counter=1
gen xent=0
gen nent=0
foreach i of varlist x330-x338{
replace xent=xent+`i'
replace nent=nent+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nenttot=`counter'-1

gen xtrans=0
foreach i of varlist x400-x413{
replace xtrans=xtrans+`i'
drop `i'
}

local counter=1
gen xser=0
gen nser=0
foreach i of varlist x380-x394{
replace xser=xser+`i'
replace nser=nser+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nsertot=`counter'-1

local counter=1
gen xnd=0
gen nnd=0
foreach i of varlist x340-x373{
replace xnd=xnd+`i'
replace nnd=nnd+1 if `i'>0
local counter=`counter'+1
drop `i'
}
gen nndtot=`counter'-1

gen xactrent=x420+x421+x422
drop x420 x421 x422
gen ximprent=x439
drop x439
gen xtax=x441
drop x441



cap: drop qx*
cap: drop hqx*

summ n*tot

aorder
save round`round'_test, replace
}




*******convert 38,43 to common
******************************
***Modify sets of goods for rounds 38,43
*****************************
**55,61,66 are fine already

foreach zz in 38 43{
use round`zz'_test, clear
foreach i in grains pulses milk oil meat veg fru sugar bev intox proc{
local max=n`i'tot[1]
forvalues j=1(1)`max'{
replace x`i'_`j'=0 if x`i'_`j'==.
replace q`i'_`j'=0 if q`i'_`j'==.
replace hx`i'_`j'=0 if hx`i'_`j'==.
replace hq`i'_`j'=0 if hq`i'_`j'==.
}
}


replace xgrains_6=xgrains_6+xgrains_1
replace qgrains_6=qgrains_6+qgrains_1
drop xgrains_1 qgrains_1 

replace xgrains_7=xgrains_7+xgrains_8
replace qgrains_7=qgrains_7+qgrains_8
drop xgrains_8 qgrains_8

replace xgrains_14=xgrains_14+xgrains_15
replace qgrains_14=qgrains_14+qgrains_15
drop xgrains_15 qgrains_15

replace xgrains_16=xgrains_16+xgrains_17
replace qgrains_16=qgrains_16+qgrains_17
drop xgrains_17 qgrains_17

replace xgrains_18=xgrains_18+xgrains_19
replace qgrains_18=qgrains_18+qgrains_19
drop xgrains_19 qgrains_19

replace xgrains_20=xgrains_20+xgrains_21
replace qgrains_20=qgrains_20+qgrains_21
drop xgrains_21 qgrains_21

replace xgrains_22=xgrains_22+xgrains_23
replace qgrains_22=qgrains_22+qgrains_23
drop xgrains_23 qgrains_23

replace xgrains_24=xgrains_24+xgrains_25
replace qgrains_24=qgrains_24+qgrains_25
drop xgrains_25 qgrains_25

replace xgrains_28=xgrains_29+xgrains_30+xgrains_31+xgrains_32+xgrains_28
replace qgrains_28=qgrains_29+qgrains_30+qgrains_31+qgrains_32+qgrains_28
drop xgrains_29 xgrains_30 xgrains_31 xgrains_32 qgrains_29 qgrains_30 qgrains_31 qgrains_32


*move to pulses
gen xpulses_2a=xgrains_26
gen qpulses_2a=qgrains_26
drop xgrains_26 qgrains_26
gen xpulses_9a=xgrains_27
gen qpulses_9a=qgrains_27
drop xgrains_27 qgrains_27

*Fix oils
replace xoil_1=xoil_1+xoil_2
replace qoil_1=qoil_1+qoil_2
drop xoil_2 qoil_2

replace xoil_6=xoil_6+xoil_7+xoil_8+xoil_9+xoil_10+xoil_12
replace qoil_6=qoil_6+qoil_7+qoil_8+qoil_9+qoil_10+qoil_12
drop xoil_7 xoil_8 xoil_9 xoil_10 xoil_12 qoil_7 qoil_8 qoil_9 qoil_10 qoil_12

*Fix meat
gen zxmeat_1=xmeat_1+xmeat_2
gen zqmeat_1=qmeat_1+qmeat_2
drop xmeat_2 qmeat_2 xmeat_1 qmeat_1

gen zxmeat_3=xmeat_3+xmeat_5
gen zqmeat_3=qmeat_3+qmeat_5
drop qmeat_5 xmeat_5 qmeat_3 xmeat_3

gen zxmeat_10=xmeat_6+xmeat_8+xmeat_10
gen zqmeat_10=qmeat_6+qmeat_8+qmeat_10
drop xmeat_10 xmeat_6 xmeat_8 qmeat_10 qmeat_6 qmeat_8

gen zxmeat_11=xmeat_11+xmeat_12
gen zqmeat_11=qmeat_11+qmeat_12
drop xmeat_11 xmeat_12 qmeat_11 qmeat_12

gen zxmeat_4=xmeat_4
gen zqmeat_4=qmeat_4
gen zxmeat_7=xmeat_7
gen zqmeat_7=qmeat_7
gen zxmeat_9=xmeat_9
gen zqmeat_9=qmeat_9
drop xmeat_4 qmeat_4 xmeat_7 qmeat_7 xmeat_9 qmeat_9

gen xmeat_1=zxmeat_9
gen qmeat_1=zqmeat_9
gen xmeat_2=zxmeat_11
gen qmeat_2=zqmeat_11
gen xmeat_3=zxmeat_1
gen qmeat_3=zqmeat_1
gen xmeat_4=zxmeat_3
gen qmeat_4=zqmeat_3
gen xmeat_5=zxmeat_4
gen qmeat_5=zqmeat_4
gen xmeat_6=zxmeat_7
gen qmeat_6=zqmeat_7
gen xmeat_7=zxmeat_10
gen qmeat_7=zqmeat_10
drop zxmeat* zqmeat*

*Vegetables
gen zxveg_8=xveg_2
gen zqveg_8=qveg_2
gen zxveg_2=xveg_8
gen zqveg_2=qveg_8
drop xveg_2 qveg_2 xveg_8 qveg_8
gen xveg_2=zxveg_2
gen qveg_2=zqveg_2
gen xveg_8=zxveg_8
gen qveg_8=zqveg_8
drop zxveg* zqveg*

replace xveg_32=xveg_32+xveg_9+xveg_17
replace qveg_32=qveg_32+qveg_9+qveg_17
drop xveg_9 xveg_17 qveg_9 qveg_17
replace xveg_22=xveg_22+xveg_23
replace qveg_22=qveg_22+qveg_23
drop xveg_23 qveg_23

*Fruits
*None

*Sugars
replace xsugar_2=xsugar_2+xsugar_3+xsugar_4
replace qsugar_2=qsugar_2+qsugar_3+qsugar_4
drop xsugar_4 qsugar_4 xsugar_3 qsugar_3

replace xsugar_1=xsugar_1+xsugar_7
replace qsugar_1=qsugar_1+qsugar_7
drop xsugar_7 qsugar_7

gen xsugar_15a=xoil_11
gen qsugar_15a=qoil_11
drop xoil_11 qoil_11

*Intoxicants
replace xintox_18=xintox_18+xintox_15
replace qintox_18=0
drop xintox_15 qintox_15









replace hxgrains_6=hxgrains_6+hxgrains_1
replace hqgrains_6=hqgrains_6+hqgrains_1
drop hxgrains_1 hqgrains_1 

replace hxgrains_7=hxgrains_7+hxgrains_8
replace hqgrains_7=hqgrains_7+hqgrains_8
drop hxgrains_8 hqgrains_8

replace hxgrains_14=hxgrains_14+hxgrains_15
replace hqgrains_14=hqgrains_14+hqgrains_15
drop hxgrains_15 hqgrains_15

replace hxgrains_16=hxgrains_16+hxgrains_17
replace hqgrains_16=hqgrains_16+hqgrains_17
drop hxgrains_17 hqgrains_17

replace hxgrains_18=hxgrains_18+hxgrains_19
replace hqgrains_18=hqgrains_18+hqgrains_19
drop hxgrains_19 hqgrains_19

replace hxgrains_20=hxgrains_20+hxgrains_21
replace hqgrains_20=hqgrains_20+hqgrains_21
drop hxgrains_21 hqgrains_21

replace hxgrains_22=hxgrains_22+hxgrains_23
replace hqgrains_22=hqgrains_22+hqgrains_23
drop hxgrains_23 hqgrains_23

replace hxgrains_24=hxgrains_24+hxgrains_25
replace hqgrains_24=hqgrains_24+hqgrains_25
drop hxgrains_25 hqgrains_25

replace hxgrains_28=hxgrains_29+hxgrains_30+hxgrains_31+hxgrains_32+hxgrains_28
replace hqgrains_28=hqgrains_29+hqgrains_30+hqgrains_31+hqgrains_32+hqgrains_28
drop hxgrains_29 hxgrains_30 hxgrains_31 hxgrains_32 hqgrains_29 hqgrains_30 hqgrains_31 hqgrains_32


*move to pulses
gen hxpulses_2a=hxgrains_26
gen hqpulses_2a=hqgrains_26
drop hxgrains_26 hqgrains_26
gen hxpulses_9a=hxgrains_27
gen hqpulses_9a=hqgrains_27
drop hxgrains_27 hqgrains_27

*Fix oils
replace hxoil_1=hxoil_1+hxoil_2
replace hqoil_1=hqoil_1+hqoil_2
drop hxoil_2 hqoil_2

replace hxoil_6=hxoil_6+hxoil_7+hxoil_8+hxoil_9+hxoil_10+hxoil_12
replace hqoil_6=hqoil_6+hqoil_7+hqoil_8+hqoil_9+hqoil_10+hqoil_12
drop hxoil_7 hxoil_8 hxoil_9 hxoil_10 hxoil_12 hqoil_7 hqoil_8 hqoil_9 hqoil_10 hqoil_12

*Fix meat
gen zhxmeat_1=hxmeat_1+hxmeat_2
gen zhqmeat_1=hqmeat_1+hqmeat_2
drop hxmeat_2 hqmeat_2 hxmeat_1 hqmeat_1

gen zhxmeat_3=hxmeat_3+hxmeat_5
gen zhqmeat_3=hqmeat_3+hqmeat_5
drop hqmeat_5 hxmeat_5 hqmeat_3 hxmeat_3

gen zhxmeat_10=hxmeat_6+hxmeat_8+hxmeat_10
gen zhqmeat_10=hqmeat_6+hqmeat_8+hqmeat_10
drop hxmeat_10 hxmeat_6 hxmeat_8 hqmeat_10 hqmeat_6 hqmeat_8

gen zhxmeat_11=hxmeat_11+hxmeat_12
gen zhqmeat_11=hqmeat_11+hqmeat_12
drop hxmeat_11 hxmeat_12 hqmeat_11 hqmeat_12

gen zhxmeat_4=hxmeat_4
gen zhqmeat_4=hqmeat_4
gen zhxmeat_7=hxmeat_7
gen zhqmeat_7=hqmeat_7
gen zhxmeat_9=hxmeat_9
gen zhqmeat_9=hqmeat_9
drop hxmeat_4 hqmeat_4 hxmeat_7 hqmeat_7 hxmeat_9 hqmeat_9

gen hxmeat_1=zhxmeat_9
gen hqmeat_1=zhqmeat_9
gen hxmeat_2=zhxmeat_11
gen hqmeat_2=zhqmeat_11
gen hxmeat_3=zhxmeat_1
gen hqmeat_3=zhqmeat_1
gen hxmeat_4=zhxmeat_3
gen hqmeat_4=zhqmeat_3
gen hxmeat_5=zhxmeat_4
gen hqmeat_5=zhqmeat_4
gen hxmeat_6=zhxmeat_7
gen hqmeat_6=zhqmeat_7
gen hxmeat_7=zhxmeat_10
gen hqmeat_7=zhqmeat_10
drop zhxmeat* zhqmeat*

*Vegetables
gen zhxveg_8=hxveg_2
gen zhqveg_8=hqveg_2
gen zhxveg_2=hxveg_8
gen zhqveg_2=hqveg_8
drop hxveg_2 hqveg_2 hxveg_8 hqveg_8
gen hxveg_2=zhxveg_2
gen hqveg_2=zhqveg_2
gen hxveg_8=zhxveg_8
gen hqveg_8=zhqveg_8
drop zhxveg* zhqveg*

replace hxveg_32=hxveg_32+hxveg_9+hxveg_17
replace hqveg_32=hqveg_32+hqveg_9+hqveg_17
drop hxveg_9 hxveg_17 hqveg_9 hqveg_17
replace hxveg_22=hxveg_22+hxveg_23
replace hqveg_22=hqveg_22+hqveg_23
drop hxveg_23 hqveg_23

*Fruits
*None

*Sugars
replace hxsugar_2=hxsugar_2+hxsugar_3+hxsugar_4
replace hqsugar_2=hqsugar_2+hqsugar_3+hqsugar_4
drop hxsugar_4 hqsugar_4 hxsugar_3 hqsugar_3

replace hxsugar_1=hxsugar_1+hxsugar_7
replace hqsugar_1=hqsugar_1+hqsugar_7
drop hxsugar_7 hqsugar_7

gen hxsugar_15a=hxoil_11
gen hqsugar_15a=hqoil_11
drop hxoil_11 hqoil_11

*Intoxicants
replace hxintox_18=hxintox_18+hxintox_15
replace hqintox_18=0
drop hxintox_15 hqintox_15






aorder


renpfix q qx
renpfix hq hqx


replace ngrainstot=0
replace ngrains=0
replace nhgrains=0
local counter=1
foreach i of varlist xgrains_2-xgrains_28{
gen zxgrains_`counter'=`i'
gen zqgrains_`counter'=q`i'
gen zhxgrains_`counter'=h`i'
gen zhqgrains_`counter'=hq`i'

local counter=`counter'+1
replace ngrainstot=ngrainstot+1
replace ngrains=ngrains+1 if `i'>0
replace nhgrains=nhgrains+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}


replace nmeattot=0
replace nmeat=0
replace nhmeat=0
local counter=1
foreach i of varlist xmeat_1-xmeat_7{
gen zxmeat_`counter'=`i'
gen zqmeat_`counter'=q`i'
gen zhxmeat_`counter'=h`i'
gen zhqmeat_`counter'=hq`i'

local counter=`counter'+1
replace nmeattot=nmeattot+1
replace nmeat=nmeat+1 if `i'>0
replace nhmeat=nhmeat+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}


replace noiltot=0
replace noil=0
replace nhoil=0
local counter=1
foreach i of varlist xoil_1-xoil_6{
gen zxoil_`counter'=`i'
gen zqoil_`counter'=q`i'
gen zhxoil_`counter'=h`i'
gen zhqoil_`counter'=hq`i'

local counter=`counter'+1
replace noiltot=noiltot+1
replace noil=noil+1 if `i'>0
replace nhoil=nhoil+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}

replace npulsestot=0
replace npulses=0
replace nhpulses=0
local counter=1
foreach i of varlist xpulses_1-xpulses_11{
gen zxpulses_`counter'=`i'
gen zqpulses_`counter'=q`i'
gen zhxpulses_`counter'=h`i'
gen zhqpulses_`counter'=hq`i'

local counter=`counter'+1
replace npulsestot=npulsestot+1
replace npulses=npulses+1 if `i'>0
replace nhpulses=nhpulses+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}

replace nsugartot=0
replace nsugar=0
replace nhsugar=0
local counter=1
foreach i of varlist xsugar_1-xsugar_16{
gen zxsugar_`counter'=`i'
gen zqsugar_`counter'=q`i'
gen zhxsugar_`counter'=h`i'
gen zhqsugar_`counter'=hq`i'

local counter=`counter'+1
replace nsugartot=nsugartot+1
replace nsugar=nsugar+1 if `i'>0
replace nhsugar=nhsugar+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}

replace nvegtot=0
replace nveg=0
replace nhveg=0
local counter=1
foreach i of varlist xveg_1-xveg_32{
gen zxveg_`counter'=`i'
gen zqveg_`counter'=q`i'
gen zhxveg_`counter'=h`i'
gen zhqveg_`counter'=hq`i'

local counter=`counter'+1
replace nvegtot=nvegtot+1 
replace nveg=nveg+1 if `i'>0
replace nhveg=nhveg+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}

replace nintoxtot=0
replace nintox=0
replace nhintox=0
local counter=1
foreach i of varlist xintox_1-xintox_18{
gen zxintox_`counter'=`i'
gen zqintox_`counter'=q`i'
gen zhxintox_`counter'=h`i'
gen zhqintox_`counter'=hq`i'

local counter=`counter'+1
replace nintoxtot=nintoxtot+1
replace nintox=nintox+1 if `i'>0
replace nhintox=nhintox+1 if h`i'>0
drop `i' q`i' h`i' hq`i'
}


renpfix qx q
renpfix hqx hq

renpfix zq q
renpfix zhq hq

renpfix zx x
renpfix zhx hx

aorder
save round`zz'_test, replace
}













foreach k in 38 43 55 61 661{
use round`k'_test, clear

***Fix remaining units

replace qlight_8=qlight_8 * 100 if round==38
replace qintox_8=qintox_8 * 100 if round==38
replace qcloth_11=qcloth_11 * 100 if round==38
replace qcloth_18=qcloth_18 * 100 if round==38
replace qcloth_19=qcloth_19 * 100 if round==38
replace hqlight_8=hqlight_8 * 100 if round==38
replace hqintox_8=hqintox_8 * 100 if round==38
cap: replace hqcloth_11=hqcloth_11 * 100 if round==38
cap: replace hqcloth_18=hqcloth_18 * 100 if round==38
cap: replace hqcloth_19=hqcloth_19 * 100 if round==38

replace qcloth_18=qcloth_18/10 if round==38
cap: replace hqcloth_18=hqcloth_18/10 if round==38

replace qintox_12=qintox_12/10 if round==43
replace hqintox_12=hqintox_12/10 if round==43


replace qproc_3=qproc_3/10 if round==50
replace hqproc_3=hqproc_3/10 if round==50

replace qintox_6=qintox_6 /(1000*1000) if round==61
replace qintox_8=qintox_8 /(1000*1000) if round==61
replace hqintox_6=hqintox_6 /(1000*1000) if round==61
replace hqintox_8=hqintox_8 /(1000*1000) if round==61

replace qintox_12=qintox_12*10 if round==61
replace hqintox_12=hqintox_12*10 if round==61

replace qintox_6=qintox_6 /(1000*1000) if round==66
replace qintox_8=qintox_8 /(1000*1000) if round==66
replace qintox_1=qintox_1*1000 if round==66
replace qcloth_19=qcloth_19*1000 if round==66
replace hqintox_6=hqintox_6 /(1000*1000) if round==66
replace hqintox_8=hqintox_8 /(1000*1000) if round==66
replace hqintox_1=hqintox_1*1000 if round==66




***Set quantities to missing for goods that do not have quantities in all rounds
***note which round they are missing in

*ice-cream - missing 50-55-61
replace qmilk_7=0
*other milk products - missing in 61,66
replace qmilk_8=0

*other meat (added in later)
replace qmeat_7=0

*other fresh fruit - missing in 55,61,66
replace qfru_16=0

**Ice
replace qbev_5=0
*Other beverages - 50,55,61,66
replace qbev_9=0

*biscuits  - 50
replace qproc_1=0
*salted refreshments - 38,43,50
replace qproc_2=0
*prepared sweets - 38, 43, 50
replace qproc_3=0
*other processed food - 50,55,61
replace qproc_9=0



*pan leaves - 61
replace qintox_1=0
*other tobacco products - 61
replace qintox_11=0
*other drugs and intoxicants - 50
replace qintox_17=0


***dung cakes - most rounds
replace qlight_4=0
***Gobar gas - missing in round 50, 61, 66
replace qlight_11=0
***other fuel and lighting?
replace qlight_12=0

***Clothing article 20 - missing 55,61,66
replace qcloth_20=0


*****Repeat for home stuff

*ice-cream - missing 50-55-61
replace hqmilk_7=0
*other milk products - missing in 61,66
replace hqmilk_8=0

*other meat (added in later)
replace hqmeat_7=0

*other fresh fruit - missing in 55,61,66
replace hqfru_16=0

**Ice
replace hqbev_5=0
*Other beverages - 50,55,61,66
replace hqbev_9=0

*biscuits  - 50
replace hqproc_1=0
*salted refreshments - 38,43,50
replace hqproc_2=0
*prepared sweets - 38, 43, 50
replace hqproc_3=0
*other processed food - 50,55,61
replace hqproc_9=0



*pan leaves - 61
replace hqintox_1=0
*other tobacco products - 61
replace hqintox_11=0
*other drugs and intoxicants - 50
replace hqintox_17=0


***dung cakes - most rounds
replace hqlight_4=0
***Gobar gas - missing in round 50, 61, 66
replace hqlight_11=0
***other fuel and lighting?
replace hqlight_12=0

***Clothing article 20 - missing 55,61,66
cap: replace hqcloth_20=0





*****What about setting home consumption equal to zero where it is forced to be zero by survey?


foreach j in 4 5 7 8 9 10 11{
replace hqgrains_`j'=0
replace hxgrains_`j'=0
}

foreach j in 11 12 13{
replace hqpulses_`j'=0
replace hxpulses_`j'=0
}

foreach j in 11 12 13{
replace hqpulses_`j'=0
replace hxpulses_`j'=0
}

foreach j in 2 3 4 5 6 7 8{
replace hqmilk_`j'=0
replace hxmilk_`j'=0
}

foreach j in 1{
replace hqoil_`j'=0
replace hxoil_`j'=0
}

foreach j in 1 2 3 5 6 7{
replace hqsugar_`j'=0
replace hxsugar_`j'=0
}

foreach j in 1 3 5 6 7 9{
replace hqbev_`j'=0
replace hxbev_`j'=0
}

foreach j in 1 2 3 4 5 6 7 8 9{
replace hqproc_`j'=0
replace hxproc_`j'=0
}


foreach j in 1 3 5 6 7 8 9 10{
replace hqlight_`j'=0
replace hxlight_`j'=0
}


*****Note that we would now have to recalculate number of home consumed items for counts
drop nh*
foreach j in grains pulses milk meat oil veg fru sugar bev proc intox light{
gen nh`j'=0
replace hx`j'=0
local nmax=n`j'tot[1]
forvalues i=1(1)`nmax'{
replace nh`j'=nh`j'+1 if hx`j'_`i'>0
replace hx`j'=hx`j'+hx`j'_`i'
}
}



*******Now generating total food expenditure and home food expenditure


gen xfood=0
gen hxfood=0
foreach i in grains pulses milk oil meat veg fru sugar bev proc{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
replace xfood=xfood+x`i'_`j'
replace hxfood=hxfood+hx`i'_`j'
}
}

gen xnonfood=xintox+xlight+xcloth+xeduc+xdur+xent+xmed+xnd+xser+xtrans
**what is excluded-- actual and imputed rent, taxes
gen totexp=xfood+xnonfood


*combine soyabeans with other pulses as it is missing from 66th round
replace qpulses_10=qpulses_8+qpulses_10
replace xpulses_10=xpulses_8+xpulses_10
drop qpulses_8 xpulses_8

replace hqpulses_10=hqpulses_8+hqpulses_10
replace hxpulses_10=hxpulses_8+hxpulses_10
drop hqpulses_8 hxpulses_8

forvalues j=8(1)12{
local i=`j'+1
gen xpulses_`j'=xpulses_`i'
gen qpulses_`j'=qpulses_`i'
gen hxpulses_`j'=hxpulses_`i'
gen hqpulses_`j'=hqpulses_`i'
drop xpulses_`i' qpulses_`i' hxpulses_`i' hqpulses_`i'
}
*redo npulsestot, npulses
replace npulsestot=12
replace npulses=0
replace nhpulses=0
forvalues j=1(1)12{
replace npulses=npulses+1 if xpulses_`j'>0
replace nhpulses=nhpulses+1 if hxpulses_`j'>0
}



**set all 0 quantities back to missing
foreach i in grains pulses milk oil meat veg fru sugar bev proc intox light cloth{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
replace q`i'_`j'=. if q`i'_`j'==0
cap: replace hq`i'_`j'=. if hq`i'_`j'==0
}
}

aorder
compress
save round`k'_edit, replace
}

